phpunit:在Laravel中验证验证异常

雷伊·杨(Rey Young)

我已经把throw Exceptionhandler.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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章