Laravel: How to enable stacktrace error on PhpUnit
Laravel: How to enable stacktrace error on PhpUnit
I have a fresh installation of laravel 5.4
I've tried to modify the default test just to see a failing test.
tests/ExampleTest.php
class ExampleTest extends TestCase
{
/**
* A basic test example.
*
* @return void
*/
public function testBasicTest()
{
$response = $this->get('/ooops');
$response->assertStatus(200);
}
}
I was expecting to see more detailed error like no route has been found or defined
etc, but instead just this error saying
no route has been found or defined
Time: 1.13 seconds, Memory: 8.00MB
There was 1 failure:
1) TestsFeatureExampleTest::testBasicTest
Expected status code 200 but received 404.
Failed asserting that false is true.
/var/www/vendor/laravel/framework/src/Illuminate/Foundation/Testing/TestResponse.php:51
/var/www/tests/Feature/ExampleTest.php:21
Its really hard to do TDD without meaningful error (yeah I know 404 in this case is enough, but most of the time its not the case).
Is there a way to enable the stacktrace the same as the one displayed on the browser? Or at least closer to that one so that I know what's the next step I should do.
Thanks in advance.
1 Answer
1
For Laravel 5.4 you can use disableExceptionHandling
method presented by Adam Wathan in this gist (source code below)
disableExceptionHandling
Now if you run in your test:
$this->disableExceptionHandling();
you should get full info that will help you to find the problem.
For Laravel 5.5 and up you can use withoutExceptionHandling
method that is built-in into Laravel
withoutExceptionHandling
Source code of Adam Wathan's gist
<?php
namespace Tests;
use AppExceptionsHandler;
use IlluminateContractsDebugExceptionHandler;
use IlluminateFoundationTestingTestCase as BaseTestCase;
abstract class TestCase extends BaseTestCase
{
use CreatesApplication;
protected function setUp()
{
/**
* This disables the exception handling to display the stacktrace on the console
* the same way as it shown on the browser
*/
parent::setUp();
$this->disableExceptionHandling();
}
protected function disableExceptionHandling()
{
$this->app->instance(ExceptionHandler::class, new class extends Handler {
public function __construct() {}
public function report(Exception $e)
{
// no-op
}
public function render($request, Exception $e) {
throw $e;
}
});
}
}
TestCase::setUp
@JaimeSangcap In fact it's not, it should be rather used in my opinion as helper when you test for errors, but you should not have it when running real tests because it might change the result and your test will be wrong depending on your app, so just use it only as a helper that helps you debug errors in test and not as a part of test
– Marcin Nabiałek
Jan 31 '17 at 14:33
yes you're right, and even without the
disableExceptionHandling
it produces meaningful errors as go. Thanks for pointing that out, that might saved me hours of hair pulling ;)– Jaime Sangcap
Feb 2 '17 at 15:18
disableExceptionHandling
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
thanks a lot, the stacktrace is showing now. It seems that I have to call it on every method. I've put it on the abstract
TestCase::setUp
method. Is there a recommended way of applying it on every test?– Jaime Sangcap
Jan 31 '17 at 9:00