ravibastola's avatar

Enable/Disable Exception handling in tests with lumen.

In laravel we can enable/disable exception handling using $this-withExceptionHandling() or $this->withoutExceptionHandling in tests. How can we achieve such behaviors in testing with lumen?

0 likes
4 replies
carbontwelve's avatar

It's a shame that this question has gone unanswered for a whole year!

In Laravel the with/without exception handling in tests is handled by the InteractsWithExceptionHandling testing "concern." See lines 61-146. This acts to cache the current exception handler and replace it with one that passes through Exceptions so you can see the stack trace and true error in your unit tests.

Lumen doesn't come shipped with InteractsWithExceptionHandling, the easiest method of obtaining its functionality is to create a InteractsWithExceptionHandling.php in your tests folder and paste in the raw source code from here.

Then in your tests where you want to be able to disable exception handling add use InteractsWithExceptionHandling;

I realise its been a year since this question was posted, but hopfully my making the effort to document a solution will help people who come here, like I did, from search engines.

7 likes
Tray2's avatar

@jakapurnama If you are using version 6 or later of Laravel use.

$this->withoutExceptionHandling();

In your tests

1 like
kamleshcgtechno's avatar

I am using Lumen 8 and my Handler.php file code is as:

public function render($request, Throwable $exception) { if($exception->getStatusCode() == 404){ return response(view("errors.404"), 404); } if($exception->getStatusCode() == 500){ return response(view("errors.500"), 500); } return parent::render($request, $exception); }

I have added 404.blade.php and 500.blade.php files in resources/views/errors folder but it is not rendering to view files. Could anyone please suggest me what is the issue and how can we solve it? Look forward to hear any suggestion. Thanks a lot.

l

1 like

Please or to participate in this conversation.