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:
-
$ laravel new app -
Go to
tests/Unit/ExampleTest.phpand changetestBasicTest()to:public function testBasicTest() { $this->get('/'); } -
Then, in
routes/web.phpchange default route to following (notice misspelleddd()function):Route::get('/', function () { d(11); }); -
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)
@morcegon call
$this->withoutExceptionHandling()at the beginning of your test method. Or in thesetUpmethod 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 didThen I believe your test will get an exception since the response will return a
500instead of a200.@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.
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
I guess this is now obsolete as https://github.com/laravel/framework/blob/master/src/Illuminate/Foundation/Testing/Concerns/InteractsWithExceptionHandling.php exists.
Thanks anyway!
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?