我正在从此页面为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?
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。