Laravel: how to enable stacktrace error on PhpUnit

I have a new installation of laravel 5.4

I tried changing the default test to see the failed 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 expected to see a more detailed error, for example, no route has been found or definedetc., but instead just this error saying

Time: 1.13 seconds, Memory: 8.00MB

There was 1 failure:

1) Tests\Feature\ExampleTest::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

It is really difficult to make TDD without a significant error (yes, I know that 404 is enough in this case, but most of the time it is not).

Is there a way to enable stacktrace in the same way as the one displayed in the browser? Or at least closer to it so that I know that I have to take the next step.

Thanks in advance.

+7
source share
2

Laravel 5.4 disableExceptionHandling ( )

, :

$this->disableExceptionHandling();

, .

Laravel 5.5 withoutExceptionHandling, Laravel

<?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;
            }
        });
    }
}
+12

Laravel 5.5 , :

$this->withoutExceptionHandling();
$this->withExceptionHandling();

setUp, . .

dump response:

/** @test */
public function it_can_delete_an_attribute()
{
    $response = $this->json('DELETE', "/api/attributes/3");

    $response->dump()->assertStatus(200);

    $this->assertDatabaseMissing('table', [
        'id' => $id
    ]);

    ...
}

laracast, .

+1

Source: https://habr.com/ru/post/1668313/


All Articles