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.
you have the withExceptionHandling()
in your test, remove it and it should work.
$response = $this->withExceptionHandling()->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
should be
$response = $this->post('/api/members', [
"name" => "Eve",
"age" => "invalid"
], ['X-Requested-With' => 'XMLHttpRequest']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With