如何断言两个 JSON 字符串表示的数据相等?
Posted
技术标签:
【中文标题】如何断言两个 JSON 字符串表示的数据相等?【英文标题】:How can I assert that data represented by two JSON strings is equal? 【发布时间】:2018-02-06 15:49:39 【问题描述】:我第一次尝试对我的项目使用单元测试,但我被一个断言模型已正确存储的测试所阻止。
这是我要测试的 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]'
其实结果是对的。但不是同一个顺序。 我在测试中做错了什么?
谢谢。
【问题讨论】:
【参考方案1】:JSON 输出只是有点不同 - 而不是比较实际的字符串输出,您最好使用 Laravel 的 JSON 断言之一来确保输出包含您期望的内容。看看the available assertions in the documentation。您可以使用$response->assertJson()
或$response->assertJsonFragment()
来确保返回所需的项目。
【讨论】:
【参考方案2】:对于您的示例,您可以使用phpunit/phpunit
附带的断言:
assertJsonFileEqualsJsonFile()
assertJsonStringEqualsJsonString()
assertJsonStringEqualsJsonFile()
或
assertEquals()
assertJsonFileEqualsJsonFile()
但是,当我——为了一个例子——将两个 JSON 字符串提取到单独的文件中
expected.json
actual.json
然后在 PhpStorm 中使用 option + command + L 格式化 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
如您所见,数据之间存在实际差异 - 根对象节点缺少值为 null
的 deleted_at
字段。
注意所以,问题归结为
您是否要断言两个 JSON 字符串相等或 你要断言两个JSON字符串表示的数据相等吗根据哪个对您很重要,选择对您更有意义的断言。
【讨论】:
谢谢! :) 最后,我将只使用 assertJson(),因为我只想确保控制器只返回一些特定的数据。以上是关于如何断言两个 JSON 字符串表示的数据相等?的主要内容,如果未能解决你的问题,请参考以下文章