【发布时间】:2017-10-14 03:44:01
【问题描述】:
我使用 laravel 5.4。在我的测试中,我会执行以下操作:
$this->get("/api/test")->assertStatus(422)->assertJson([
"status" => "ok"
]);
当状态码为 422 时,我得到了一个 HttpException,尽管我已经完全准备好了——而且我什至期待它。我的问题是异常阻止了我的断言,因此assertJson 不起作用。
$this->expectException(HttpException::class) 没有帮助,因为它捕获了这个异常并停止了,所以这开始通过了:
$this->expectException(HttpException::class)
$this->get("/api/test")->assertStatus(422)->assertJson([
"status" => "ok"
]);
$this->assertTrue(false);
我知道我可以自己捕获这个异常,但是我将无法以简单的方式将我的断言应用于响应。我该如何解决这个问题?
【问题讨论】:
-
也许这种方法会有所帮助stackoverflow.com/questions/36835671/…
-
@AlexSlipknot 呃,没有找到
action方法=c -
我觉得你可以试试方法
->assertResponseStatus(422);见laracasts.com/discuss/channels/testing/…