framework: [5.4] TestResponse does not display a thrown exception

  • Laravel Version: 5.4.9
  • PHP Version: 7.1.1
  • Database Driver & Version: N/A

Description:

When testing an endpoint that returns an exception, Laravel does not show this exception and phpunit returns green for the test.

I believe that this is caused by Illuminate\Foundation\Testing\TestResponse::fromBaseResponse() which stores the exception on a TestResponse object but, by default, does not display it in the console.

I am not entirely sure if this is a bug. Although, when I test an endpoint that throws an exception, I’d like to know about it.

Steps To Reproduce:

  1. $ laravel new app

  2. Go to tests/Unit/ExampleTest.php and change testBasicTest() to:

    public function testBasicTest()
    {
        $this->get('/');
    }
    
  3. Then, in routes/web.php change default route to following (notice misspelled dd() function):

    Route::get('/', function () {
        d(11);
    });
    
  4. run $ phpunit - you should see the tests passing. There is no indication that the function is misspelled.

Thanks.

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 15 (11 by maintainers)

Most upvoted comments

@morcegon call $this->withoutExceptionHandling() at the beginning of your test method. Or in the setUp method to disable exception handling for all your tests.

@jerguslejko when calling $this->get('/') you aren’t making any specific assertions against the response which is why you aren’t getting any failing tests. If you instead did

$this->get('/')->assertResponseOk();

Then I believe your test will get an exception since the response will return a 500 instead of a 200.

@themsaid I think what you were referring to was this tweet which will re-throw your exceptions instead of having the framework handle them and return a valid response. This was warned against by Adam Wathan however so use with caution.

image

Hi @jerguslejko . Trying to add $this->withoutExceptionHandling() method at the beginning of my test, throws a call to undefined method on Lumen 5.6. Do i have any other option to activate this feature?

Hi @jerguslejko . Trying to add $this->withoutExceptionHandling() method at the beginning of my test, throws a call to undefined method on Lumen 5.6. Do i have any other option to activate this feature?

I wrote manual helper in abstract TestCase class

public function disableExceptionHandling(){
     app()->instance(\App\Exceptions\Handler::class, new class extends \App\Exceptions\Handler{
         public function __construct () { }
         public function report ( Exception $exception ) {
             parent ::report( $exception ); // TODO: Change the autogenerated stub
         }

         public function render($request, Exception $exception)
         {
             throw $exception;
         }
     });
}

Well, as much as I agree with you I still think that it would be much more useful to see actual error “by default”.

While your solution helps, it requires some additional work + there are situations, as Adam Wathan mentions in the tweet, where you want the exception to be handled by the framework.

The “philosophical thing” is whether a test should pass if the response status was 500.

Any opinions @themsaid @taylorotwell ? thanks

@themsaid I guess this https://github.com/laravel/framework/blob/5.4/src/Illuminate/Foundation/Testing/TestResponse.php#L36 could be changed to something like?

if (isset($response->exception)) {
    try {
        throw $response->exception;
    } catch (SomethingExpected $e) {
        $testResponse->exception = $response->exception;
    }
}