我第一次尝试对我的项目使用单元测试,但是我阻止了测试声明模型已正确存储。
这是我要测试的API控制器:
public function store(QuestionFormRequest $request)
{
$questionRequest = $request->question();
$question = new Question($questionRequest);
$question->save();
$question->answers()->createMany($questionRequest['answers']);
return response()->json($question->load('answers'), 201);
}
这是我的测试:
public function it_can_store_a_question()
{
$surveyFactory = factory(Survey::class)->create();
$themeFactory = factory(Theme::class)->create();
$pillarFactory = factory(Pillar::class)->create();
$questionToStore = [
'survey_id' => $surveyFactory->id,
'theme_id' => $themeFactory->id,
'pillar_id' => $pillarFactory->id,
'name' => 'question',
'type' => 'simple',
'answers' => [
[
'label' => 'reponse1',
'points' => '3',
],
[
'label' => 'reponse2',
'points' => '5',
]
]
];
$response = $this->post('/api/1.0/question', $questionToStore);
$response->assertStatus(201);
$expectedQuestion = Question::with('answers')->get()->first();
$this->assertEquals(json_encode($expectedQuestion), $response->getContent());
}
结果如下:
Failed asserting that two strings are equal.
Expected :'{"id":1,"survey_id":1,"theme_id":1,"pillar_id":1,"name":"question","type":"simple","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null,"answers":[{"id":1,"question_id":1,"label":"reponse1","points":3,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null},{"id":2,"question_id":1,"label":"reponse2","points":5,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null}]}'
Actual :'{"survey_id":1,"theme_id":1,"pillar_id":1,"name":"question","type":"simple","updated_at":"2017-08-29 08:54:45","created_at":"2017-08-29 08:54:45","id":1,"answers":[{"id":1,"question_id":1,"label":"reponse1","points":3,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null},{"id":2,"question_id":1,"label":"reponse2","points":5,"description":"","created_at":"2017-08-29 08:54:45","updated_at":"2017-08-29 08:54:45","deleted_at":null}]}'
事实上,结果是正确的。但不是按照相同的顺序。 我的测试中我做错了什么?
感谢。
答案 0 :(得分:3)
对于您的示例,您可以使用随phpunit/phpunit
附带的断言:
或
assertJsonFileEqualsJsonFile()
然而,当我 - 为了举例 - 将两个JSON字符串提取到单独的文件
expected.json
actual.json
然后使用选项 + 命令 + L 在PhpStorm中格式化JSON并运行以下测试
use PHPUnit\Framework;
final class JsonTest extends Framework\TestCase
{
public function testJsonEquals()
{
$expected = __DIR__ . '/expected.json';
$actual = __DIR__ . '/actual.json';
$this->assertJsonFileEqualsJsonFile($expected, $actual);
}
}
测试失败
Failed asserting that '{\n
"id": 1,\n
"survey_id": 1,\n
"theme_id": 1,\n
"pillar_id": 1,\n
"name": "question",\n
"type": "simple",\n
"created_at": "2017-08-29 08:54:45",\n
"updated_at": "2017-08-29 08:54:45",\n
"deleted_at": null,\n
"answers": [\n
{\n
"id": 1,\n
"question_id": 1,\n
"label": "reponse1",\n
"points": 3,\n
"description": "",\n
"created_at": "2017-08-29 08:54:45",\n
"updated_at": "2017-08-29 08:54:45",\n
"deleted_at": null\n
},\n
{\n
"id": 2,\n
"question_id": 1,\n
"label": "reponse2",\n
"points": 5,\n
"description": "",\n
"created_at": "2017-08-29 08:54:45",\n
"updated_at": "2017-08-29 08:54:45",\n
"deleted_at": null\n
}\n
]\n
}\n
' matches JSON string "{
"survey_id": 1,
"theme_id": 1,
"pillar_id": 1,
"name": "question",
"type": "simple",
"updated_at": "2017-08-29 08:54:45",
"created_at": "2017-08-29 08:54:45",
"id": 1,
"answers": [
{
"id": 1,
"question_id": 1,
"label": "reponse1",
"points": 3,
"description": "",
"created_at": "2017-08-29 08:54:45",
"updated_at": "2017-08-29 08:54:45",
"deleted_at": null
},
{
"id": 2,
"question_id": 1,
"label": "reponse2",
"points": 5,
"description": "",
"created_at": "2017-08-29 08:54:45",
"updated_at": "2017-08-29 08:54:45",
"deleted_at": null
}
]
}
".
--- Expected
+++ Actual
@@ @@
{
- "id": 1,
"survey_id": 1,
"theme_id": 1,
"pillar_id": 1,
"name": "question",
"type": "simple",
+ "updated_at": "2017-08-29 08:54:45",
"created_at": "2017-08-29 08:54:45",
- "updated_at": "2017-08-29 08:54:45",
- "deleted_at": null,
+ "id": 1,
你可以看到的是,实际上两个JSON字符串不相等,至少字段的顺序是不一样的。断言输出的问题在于它并不是真的有用,只是因为断言只比较了两个字符串。
assertEquals()
但是,当我们json_decode()
文件的内容然后使用assertEquals()
断言时,这样:
use PHPUnit\Framework;
final class JsonTest extends Framework\TestCase
{
public function testJsonEquals()
{
$expected = json_decode(file_get_contents(__DIR__ . '/expected.json'), true);
$actual = json_decode(file_get_contents(__DIR__ . '/actual.json'), true);
$this->assertEquals($expected, $actual);
}
}
然后测试再次失败,但输出实际上很多更有帮助:
Failed asserting that two arrays are equal.
--- Expected
+++ Actual
@@ @@
- 'deleted_at' => null
如您所见,数据之间存在实际差异 - 根对象节点缺少值deleted_at
的{{1}}字段。
注意所以,问题归结为
根据对您来说重要的内容,选择对您更有意义的断言。
答案 1 :(得分:2)
JSON输出略有不同 - 不是比较实际的字符串输出,而是使用Laravel的JSON断言之一,以确保输出包含您期望的内容。看看the available assertions in the documentation。您可以使用$response->assertJson()
或$response->assertJsonFragment()
来确保返回所需的项目。