$data = [
'name' => '', // required field
'email' => '' // required field
];
// or $data = [];
$this->postJson('api/users', $data)
->assertStatus(422)
->assertJsonValidationErrors(
['name']
);
I am writing tests for validating the fields. The above test will pass, which I expect to fail because name and email fields are both required and should be both in the validation errors. How can I force assertJsonValidationErrors to check all fields in the error bag. Thanks.
After some thinking and digging into laravel documentation, I found a solution to it: use assertJson
instead of assertJsonValidationErrors
$data = [
'name' => '', // required field
'email' => '' // required field
];
// or $data = [];
$this->postJson('api/users', $data)
->assertStatus(422)
->assertJson(function (AssertableJson $json) use ($data) {
$json->has('message')
->has('errors', 2) // it will check the exact size of the errors bag
->whereAllType([
'errors.name' => 'array',
'errors.email' => 'array',
]);
});