Laravel: How to enable stacktrace error on PhpUnit
For Laravel 5.4 you can use disableExceptionHandling
method presented by Adam Wathan in this gist (source code below)
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
Source code of Adam Wathan's gist
<?php
namespace Tests;
use App\Exceptions\Handler;
use Illuminate\Contracts\Debug\ExceptionHandler;
use Illuminate\Foundation\Testing\TestCase 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;
}
});
}
}
If you happen to use Laravel 5.5 and up, you can use the built-in methods:
$this->withoutExceptionHandling();
$this->withExceptionHandling();
Either in your setUp method, or in your test method's. They are defined in the following trait.
For quick and dirty debugging, you can also use the dump
method on the response
object:
/** @test */
public function it_can_delete_an_attribute()
{
$response = $this->json('DELETE', "/api/attributes/3");
$response->dump()->assertStatus(200);
$this->assertDatabaseMissing('table', [
'id' => $id
]);
...
}
There is a laracast lesson that covers these details.