Laravel assertJson失败
我对此测试有一个问题:Laravel assertJson失败,laravel,phpunit,Laravel,Phpunit,我对此测试有一个问题: $this->json('POST', 'api/login') ->assertStatus(422) ->assertJson([ 'email' => ['The email field is required.'], 'password' => ['The password field is required.'], ]); 我不明白错误
$this->json('POST', 'api/login')
->assertStatus(422)
->assertJson([
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
]);
我不明白错误是什么:
Unable to find JSON:
[{
"email": [
"The email field is required."
],
"password": [
"The password field is required."
]
}]
within response JSON:
[{
"message": "The given data was invalid.",
"errors": {
"email": [
"The email field is required."
],
"password": [
"The password field is required."
]
}
}].
Failed asserting that an array has the subset Array &0 (
'email' => Array &1 (
0 => 'The email field is required.'
)
'password' => Array &2 (
0 => 'The password field is required.'
)
).
--- Expected
+++ Actual
@@ @@
0 => 'The password field is required.',
),
),
- 'email' =>
- array (
- 0 => 'The email field is required.',
- ),
- 'password' =>
- array (
- 0 => 'The password field is required.',
- ),
)
JSON断言似乎在答案中。
assertJson
在这种情况下不起作用,因为您要查找的数据在错误下
您可以包装数组并在其中键入“errors”
:
或者您也可以使用以下选项,尝试将json的任何部分与您提供的内容进行匹配:
->assertJsonFragment([
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
])
你用的是什么版本的Laravel?我用的是Laravel 5.8.15。我使用assertJsonFragment进行求解
->assertJsonFragment([
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
])