I have the following test:
public function testStoreMemberValidation()
{
$response = $this->withExceptionHandling()->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
dd($response->json());
};
I am trying to assert that the response is of the form of a validation error. The controller method is as follows:
public function store(Request $request)
{
$data = $request->validate([
'name' => 'required|string',
'age' => 'required|integer',
]);
Member::create($data);
}
However, whenever I call any assertion which calls $response->json()
(which is most of them) I get an exception:
Illuminate\Validation\ValidationException : The given data was invalid.
How can I perform assertions on this response without throwing this error?
Note, I am using Laravel 5.7.
$this->withoutExceptionHandling
. – Aniseed$this->postJson()
to avoid adding theX-Requested-With
header manually, and you can test for validation errors using$response->assertJsonValidationErrors('field_name')
. – Aniseed$this->postJson()
seems to have fixed it. I'm fairly sure I haven't called$this->withoutExceptionHandling()
anywhere though. – Cards