Phpunit:在laravel中隐藏验证异常
我在Phpunit:在laravel中隐藏验证异常,laravel,laravel-5,phpunit,laravel-testing,Laravel,Laravel 5,Phpunit,Laravel Testing,我在handler.php中打开了throw Exception,这样我就可以捕获异常,并查看错误,但发生的情况是,当我尝试执行验证检查时它会抛出一个正确的异常,但在我的测试用例中,它不会捕获我正在断言的异常会话有错误 /** @test*/ public function a_thread_requires_a_title(){ $thread = make('App\Thread', ['title'=> null]); $this->p
handler.php中打开了throw Exception
,这样我就可以捕获异常
,并查看错误
,但发生的情况是,当我尝试执行验证检查时
它会抛出一个正确的异常,但在我的测试用例中,它不会捕获我正在断言的异常会话有错误
/** @test*/
public function a_thread_requires_a_title(){
$thread = make('App\Thread', ['title'=> null]);
$this->post('threads', $thread->toArray())
->assertSessionHasErrors('title');
}
因为,validation error
是一个异常,所以它会抛出一个异常,因为我将handler.php
文件修改为
if(app()->environment() === "testing") throw $exception;
因此,我要做的是更改这一测试的env,这样它就不会向我抛出“异常”有两个助手方法,您可以在测试方法的顶部编写它们:
$this->withoutExceptionHandling()
和$this->withExceptionHandling()代码>
它们包含在Laravel的“illumb\Foundation\Testing\Concerns\interactionswithExceptionHandling”特性中,该特性由抽象测试用例使用,您应该从测试中扩展。()
将其放在测试方法的顶部:-$this->withoutExceptionHandling()
withoutExceptionhandling其已内置?@senty此方法不适用于我。。我仍然得到验证异常。?有办法吗?天哪。。。。。还有例外处理。。对不起,我在学老课程。。。恰恰相反。。谢谢一群人:)不用担心-快乐编码
/** @test*/
public function a_thread_requires_a_title() {
$this->withExceptionHandling();
$thread = make('App\Thread', ['title'=> null]);
$this->post('threads', $thread->toArray())
->assertSessionHasErrors('title');
}