微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

我有一个全新的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);
    }
}

我期待看到更详细的错误,如没有找到或定义的路线等,但只是这个错误

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就足够了,但大部分时间都不是这样).

有没有办法使堆栈跟踪与浏览器上显示的相同?或者至少接近那个,以便我知道我应该做的下一步是什么.

提前致谢.

对于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;
            }
        });
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。