laravel - Laravel:如何在PhpUnit上启用stacktrace错误

标签 laravel laravel-5 phpunit tdd laravel-5.4

我重新安装了laravel 5.4

我试图修改默认测试只是为了查看失败的测试。

测试/ExampleTest.php

class ExampleTest extends TestCase
{
    /**
     * A basic test example.
     *
     * @return void
     */
    public function testBasicTest()
    {
        $response = $this->get('/ooops');

        $response->assertStatus(200);
    }
}


我期待看到更详细的错误,例如no route has been found or defined等,但只是此错误说

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


很难做到没有有意义的错误的TDD(是的,我知道404在这种情况下就足够了,但是在大多数情况下不是这样)。

有没有一种方法可以使stacktrace与浏览器上显示的相同?或者至少更接近那个,以便我知道下一步应该做什么。

提前致谢。

最佳答案

对于Laravel 5.4,您可以使用Adam Wathan在this gist中提供的disableExceptionHandling方法(下面的源代码)

现在,如果您在测试中运行:

$this->disableExceptionHandling();


您应该获得完整的信息,以帮助您发现问题。

对于Laravel 5.5及更高版本,您可以使用Laravel内置的withoutExceptionHandling方法

亚当·沃森(Adam Wathan)的要旨的源代码

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

关于laravel - Laravel:如何在PhpUnit上启用stacktrace错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41945622/

相关文章:

Laravel:如何使用 Artisan Facade 在 Controller 和模型中调用终端命令

laravel - 如何从本地网络访问 laravel 代客项目?

php - 如何在默认用户模型上设置 Eloquent 关系

php - Laravel:未找到 Layouts.master

php - 什么是 .phpunit.result.cache

php - disableOriginalConstructor 有什么作用?

Laravel:collect() 助手 - 设置集合类型

mysql - 我们如何使用 vue js 从数据库中检索产品数量?

mysql - 如何在 LARAVEL 5.6 中一起使用 GROUP BY 和 ORDER BY

Laravel 迁移更改/更新