【发布时间】:2019-08-07 10:52:42
【问题描述】:
我已将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;
所以,我想做的是改变这个测试的环境,这样它就不会抛出一个“异常”
【问题讨论】:
-
把它放在你的测试方法的顶部:-
$this->withoutExceptionHandling(); -
withoutExceptionhandling 它已经内置了?
-
@senty 这个方法对我不起作用。我仍然收到验证异常。?有办法吗?
-
Omg ..... 也有 withExceptionHandling .. 对不起,我在学习旧课程... 恰恰相反.. 谢谢一群人
-
:) 不用担心 - 快乐编码
标签: laravel laravel-5 phpunit laravel-testing