json - 如何断言两个 JSON 字符串表示的数据相等?

标签 json laravel unit-testing phpunit

我第一次尝试对我的项目使用单元测试,但我被断言模型已正确存储的测试所阻止。

这是我要测试的 API Controller :

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}]}'

其实结果是对的。但顺序不一样。 我在测试中做错了什么?

谢谢。

最佳答案

对于您的示例,您可以使用 phpunit/phpunit 附带的断言:

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

如您所见,数据之间存在实际差异 - 根对象节点缺少值为 nulldeleted_at 字段。

注意 所以,问题归结为

  • 你想断言两个 JSON 字符串是相等的还是
  • 是否要断言两个 JSON 字符串表示的数据相等

根据哪个对您重要,选择对您更有意义的断言。

关于json - 如何断言两个 JSON 字符串表示的数据相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45934857/

相关文章:

javascript - 为什么 JavaScript 不显示解析后的 json 数据?

android - 如何使用 GSON 解析此 JSON 数据?并将其放入 ArrayList

php - MySql 无法在文本搜索中决定 LIKE 和 MATCH

angularjs - Jasmine SpyOn 多次使用相同的方法

json - Go中根据字符串动态创建某种类型的变量

php - FullCalendar JSON Feed 不起作用 : my calendar doesn't put the data from the JSON feed onto my webpage

Laravel FFMpeg - FFMpeg 无法就地编辑现有文件

Laravel 列名和关系名相同

c# - AutoFixture 构建-卡住-创建序列

reactjs - 使用 header 通过 fetch-mock 模拟获取请求