我对这个测试有问题:
$this->json('POST', 'api/login')
->assertStatus(422)
->assertJson([
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
]);
Run Code Online (Sandbox Code Playgroud)
我不明白错误是什么:
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.',
- ),
)
Run Code Online (Sandbox Code Playgroud)
似乎 JSON 断言在答案之内。
assertJson在这种情况下将不起作用,因为您要查找的数据位于errors.
您可以包装您的数组并使用以下键将其键入"errors":
->assertJson([
'errors' => [
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
],
])
Run Code Online (Sandbox Code Playgroud)
或者您可以改为使用assertJsonFragment,它将尝试将 json 的任何部分与您提供的内容进行匹配:
->assertJsonFragment([
'email' => ['The email field is required.'],
'password' => ['The password field is required.'],
])
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1011 次 |
| 最近记录: |