开发者社区> 问答> 正文

assertJson()用于Laravel中的JSON数组

我正在从此页面为Laravel重新创建一个简单的api:https ://www.toptal.com/laravel/restful-laravel-api-tutorial?utm_campaign = blog_post_restful_laravel_api_tutorial&utm_medium = email&utm_source = blog_subscribers

在测试阶段,我正在忙于中的testArticlesAreListedCorrectly()功能问题ArticleTest.php。该代码在页面中并不完美,就像我必须更改为jsonAssert()才能jsonAssertFragment()使测试正常进行一样。但是在响应中声明多个Json数组似乎很困难,并且显示错误。有没有一种方法可以同时对两个数组使用JsonFragment,同时检查它们是否正确列出?

{

    factory(Article::class)->create([
        'title' => 'First Article',
        'body' => 'First Body'
    ]);

    factory(Article::class)->create([
        'title' => 'Second Article',
        'body' => 'Second Body'
    ]);

    $user = factory(User::class)->create();
    $token = $user->generateToken();
    $headers = ['Authorization' => "Bearer $token"];

    $response = $this->json('GET','/api/articles', [], $headers)
        ->assertStatus(200)
        ->assertJson([
            ['title' => 'First Article', 'body' => 'First Body'],
            ['title' => 'Second Article', 'body' => 'Second Body']
        ])
        ->assertJsonStructure([
            '*' => ['id', 'body', 'title', 'created_at', 'updated_at'],
        ]);
}

错误:


Time: 23.53 seconds, Memory: 28.00 MB

There was 1 failure:

1) Tests\Feature\ArticleTest::testArticlesAreListedCorrectly
Unable to find JSON: 

[[
    {
        "title": "First Article",
        "body": "First Body"
    },
    {
        "title": "Second Article",
        "body": "Second Body"
    }
]]

within response JSON:

[[

        "id": 51,
        "title": "First Article",
        "body": "First Body",
        "created_at": "2019-11-27 06:01:13",
        "updated_at": "2019-11-27 06:01:13"
    },
    {
        "id": 52,
        "title": "Second Article",
        "body": "Second Body",
        "created_at": "2019-11-27 06:01:13",
        "updated_at": "2019-11-27 06:01:13"
    }
]].


Failed asserting that an array has the subset Array &0 (
    0 => Array &1 (
        'title' => 'First Article'
        'body' => 'First Body'
    )
    1 => Array &2 (
        'title' => 'Second Article'
        'body' => 'Second Body'
    )
).
--- Expected
+++ Actual
@@ @@
   0 => 
   array (
     'id' => 1,
-    'title' => 'First Article',
-    'body' => 'First Body',
+    'title' => 'Modi distinctio officiis similique quis ut suscipit facilis.',
+    'body' => 'Reiciendis iste beatae rerum dolore nesciunt ullam est. Accusamus autem eaque nihil voluptates suscipit incidunt. Et voluptas voluptas non sint.',
     'created_at' => '2019-11-27 06:01:12',
     'updated_at' => '2019-11-27 06:01:12',
   ),
@@ @@
   1 => 
   array (
     'id' => 2,
-    'title' => 'Second Article',
-    'body' => 'Second Body',
+    'title' => 'Suscipit soluta rerum officia voluptatem sint ducimus.',
+    'body' => 'Ut dolor dolorem voluptas sint et alias. Dolores ipsa quasi nisi enim aperiam tenetur. Sed expedita sit impedit at eius quod. Dolore eaque officiis vero consequuntur sit.',
     'created_at' => '2019-11-27 06:01:12',
     'updated_at' => '2019-11-27 06:01:12',
   ),```

Any idea how I can run this test?

展开
收起
社区秘书 2019-11-27 16:30:58 690 0
0 条回答
写回答
取消 提交回答
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载

相关实验场景

更多