If I want to validate that given input is a valid array, I used to write my tests something like this.
$this->postJson("/api/items", [
'items' => 'not-an-array'
])
->assertStatus(422)
->assertJsonValidationErrors('items');
This gives a false positive result in some situations. Test does not know which validation rule is failing.
Here is the better way to write your test.
$this->postJson("/api/items", [
'items' => 'not-an-array'
])
->assertStatus(422)
->assertJsonValidationErrors(['items' => 'array']);
This assert if the array keyword exist in the validation error message. Now I am confident that my test is passing because items
is not an array.
Top comments (0)