我已经把throw Exception
在handler.php
这样我就可以赶上exceptions
和看到的errors
,但发生的事情是,当我尝试做validation checks
它抛出我一个例外是正确的,但在我的测试案例,而不是追赶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;
因此,我要尝试的是更改此测试的环境,以免引发“异常”
您可以在测试方法的顶部编写2种辅助方法:
$this->withoutExceptionHandling();
和 $this->withExceptionHandling();
它们包含在Laravel的“ Illuminate \ Foundation \ Testing \ Concerns \ InteractsWithExceptionHandling”特征中,该特征由抽象TestCase使用,您应该从测试中进行扩展。(如此处所述)
/** @test*/
public function a_thread_requires_a_title() {
$this->withExceptionHandling();
$thread = make('App\Thread', ['title'=> null]);
$this->post('threads', $thread->toArray())
->assertSessionHasErrors('title');
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句