Mohamed Allam
Laravel Example

Laravel Example

Laravel Testing Exception handling

Laravel Testing Exception handling

Laravel comes with Exception handling, however this can impact the clarity of your tests, let's fix just that.

Mohamed Allam's photo
Mohamed Allam

Published on Sep 14, 2021

3 min read

Subscribe to my newsletter and never miss my upcoming articles

Exception handling.

wikipedia

In computing and computer programming, exception handling is the process of responding to the occurrence of exceptions – anomalous or exceptional conditions requiring special processing – during the execution of a program.

In other words, when our code breaks, and we throw an exception.

Laravel testing and exception handling

Laravel features tests assumes you want Exception Handling enabled. Which is the case most of the time. However there is many cases, where we dont want the exception handling enabled.

in many tests you might end with. 500 instead of 200, so it does give you the header code, but wont give you proper response for the actual issue you have, lets give an example, the basic example test when we install Laravel. is this test.

public function test_example()
{
    $response = $this->get('/');

    $response->assertStatus(200);
}

Lets just without create a route or anything, try to visit a route that dosent exist.

public function test_example()
{
    $response = $this->get('/example-error');

    $response->assertStatus(200);
}

There was a failure, Our code squaking:

Expected response status code [200] but received 404. Failed asserting that 200 is identical to 404.

Laravel without Exception Handling

In this example If we visit the given route on a " http://laravelexample.test/example-error" on our page, we would catch the NotFoundHttpException.

However 404 wont help us understand what portion of our code broke, or what exactly is missing. Laravel ships with lot of magic, So this is a no exception. Adam Wathan introduced a way, to toggle exception handling in tests, around Laravel 5.1.

So now we can set it to on or off, Laravel tests defaults to Exception Handling. So lets fix that, we can set it off

public function test_example()
{
    $this->withoutExceptionHandling();
    $response = $this->get('/example-error');

    $response->assertStatus(200);
}

Now we get the proper error we were looking for.

Symfony\Component\HttpKernel\Exception\NotFoundHttpException: GET http://laravelexample.test/example-error

Laravel with Exception Handling

Even better we can set up the exception handling back on.

public function test_example()
{
    $this->withoutExceptionHandling();
    $response = $this->get('/example-error');
    $this->withExceptionHandling();

    $response->assertStatus(200);
}

Multiple requests in a signle test

You might wonder why would we need it back as it wont matter after the error, specially the exception is thrown on the excustion of the get request. and not in the assertions. in other words.

public function test_example()
{
    $this->withoutExceptionHandling();
    $this->get('/example-error');

}

This will give us the same HttpNotFoundException. Many times we run multiple http requests in one test, sometimes we want some with exception handling and others without it. Or sometimes we want to just toggle off exception handling optionally, we comment and uncomment it depending on our test, and results.

There for we have it, Thanks to Laravel and Adam Wathan.

 
Share this
Proudly part of