How to test custom exception handling
Hi there!
I'm using inertia with my laravel project and implement a modified exception handling as suggested by inertia:
/**
* Render the exception into an HTTP response.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function render($request, Throwable $e)
{
$response = parent::render($request, $e);
if ((!app()->environment(['local', 'testing']) && in_array($response->status(), [500])) || in_array($response->status(), [403, 404, 503])) {
return \Inertia::render('app.error.show', [
'auth' => 'test123',
'inertiaEnv' => 'app',
'errors' => [
'code' => $response->status(),
'text' => $response->statusText()
]
])
->toResponse($request)
->setStatusCode($response->status());
} else if ($response->status() === 419) {
return back()->with([
'message' => 'The page expired, please try again.',
]);
}
return $response;
}
I want now implement a test which catch the response and check, the expected values are existing:
public function test_error_handler_return_expected()
{
$response = $this->get('/unknownPage');
$response->assertStatus(404);
$response->assertJson([
'component' => 'app.error.show',
'props' => [
'auth' => 'test123',
'inertiaEnv' => 'app',
'errors' => [
'code' => '404',
'text' => 'Not found'
],
],
'url' => '/unknownPage'
]);
}
Problem is now, that the response check are never reached. So I far I understands, it's because exception is thrown. But with $this->withoutExceptionHandling(); it sill does not pass the response checks. I also try to catch the exception with try {} catch(\Exception $e) {} but in that situation i did not get the response from the previous call. What I need is simply url call without any exception handling. Is there anything?
Pragmatic solution could be, using guzzle directly because there I should get the response, but it isn't full integrated into the testing environment.
Please or to participate in this conversation.