禁用测试的Laravel异常处理 [英] Disable Laravel exception handling for Tests
问题描述
我正在遵循本课程 testdrivenlaravel ,其中提到了一种禁用Laravel异常处理的方法,以防止Laravel处理发生的异常并将其抛出,因此我们可以在中获取更详细的错误.我们的测试输出.
I am following along this course testdrivenlaravel, and it mentions a way disable the Laravel's exception handling to prevent Laravel from handling exceptions that occur and instead throwing it, so we can get a more detailed error in our tests output.
所以我在测试用例类中添加了此方法,并且在render方法中抛出了异常
So I added this method in my testcase class, and in the render method I am throwing the exception
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;
}
});
}
但是,每当我在测试中调用它时,为了获得更详细的错误,我仍然会得到与Laravel Handler呈现的错误相同的错误.
But whenever I call it in a test, to get more detailed error, I still get the same errors that Laravel Handler is rendering.
当我像这样直接更改Handler
类时:
When I change the Handler
class directly like this:
public function render($request, Exception $exception)
{
throw $exception;
// return parent::render($request, $exception);
}
我得到了详细的错误,但是我需要得到disableExceptionHandling
助手的工作.
I get the detailed errors, but I need to get the disableExceptionHandling
helper work.
推荐答案
将其放在测试方法的顶部:
Put this at the top of your test method:
$this->withoutExceptionHandling();
您不需要为此创建方法,它包含在laravel的'InteractsWithExceptionHandling'特性中,该特性由抽象TestCase使用,您应该从测试中进行扩展.
You don't need to create a method for this, it's included in laravel's 'InteractsWithExceptionHandling' trait which is used by the abstract TestCase that you should be extending from your test.
这篇关于禁用测试的Laravel异常处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!