禁用测试的Laravel异常处理

Mub*_*bas 3 tdd laravel

我遵循的是testdrivenlaravel课程,它提到了一种禁用Laravel异常处理的方法,以防止Laravel处理发生的异常并抛出该异常,因此我们可以在测试输出中得到更详细的错误。

所以我在测试用例类中添加了此方法,然后在render方法中抛出了异常

protected function disableExceptionHandling() {

    $this->app->instance(Handler::class, new class extends Handler {
        public function __construct()
        {
        }
        public function report(\Exception $e)
        {
        }
        public function render($request, \Exception $e)
        {
            throw $e;
        }
    });
}
Run Code Online (Sandbox Code Playgroud)

但是,每当我在测试中调用它时,为了获得更详细的错误,我仍然会得到与Laravel Handler呈现的错误相同的错误。

当我Handler像这样直接更改类时:

public function render($request, Exception $exception)
{
    throw $exception;
    // return parent::render($request, $exception);
}
Run Code Online (Sandbox Code Playgroud)

我得到了详细的错误,但是我需要完成disableExceptionHandling助手的工作。

whe*_*ker 8

将其放在测试方法的顶部:

    $this->withoutExceptionHandling();
Run Code Online (Sandbox Code Playgroud)

您不需要为此创建方法,它包含在laravel的'InteractsWithExceptionHandling'特性中,该特性由抽象TestCase使用,您应该从测试中扩展该特性。