0

I have a unit test for a function that adds data (untransformed) to the database. The data to insert is given to the create function. Do I use the input data in my asserts or is it better to specify the data that I’m asserting?

For eample:

$personRequest = [ 'name'=>'John', 'age'=>21, ]; $id = savePerson($personRequest); $personFromDb = getPersonById($id); $this->assertEquals($personRequest['name'], $personFromDb['name']); $this->assertEquals($personRequest['age'], $personFromDb['age']); 

Or

$id = savePerson([ 'name'=>'John', 'age'=>21, ]); $personFromDb = getPersonById($id); $this->assertEquals('John', $personFromDb['name']); $this->assertEquals(21, $personFromDb['age']); 

2 Answers 2

1

I think 1st option is better. Your input data may change in future and if you go by 2nd option, you will have to change assertion data everytime.

2nd option is useful, when your output is going to be same irrespective of your input data.

Sign up to request clarification or add additional context in comments.

Comments

0

I got an answer from Adam Wathan by e-mail. (i took his test driven laravel course and noticed he uses the 'specify' option)

I think it's just personal preference, I like to be able to visually skim and see "ok this specific string appears here in the output and here in the input", vs. trying to avoid duplication by storing things in variables." Nothing wrong with either approach in my opinion!

So i can't choose a correct answer.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.