【发布时间】:2018-11-23 16:03:06
【问题描述】:
我在 laravel 5.5 项目中有奇怪的行为。我已经设置并运行了一些功能测试,如果传入的 id 不存在,则需要测试特定路由是否会返回 404。我在RouteServiceProvider 中为我的Note 模型设置了显式模型绑定
Route::bind('note', function($value){
return Note::where('id', $value)->first() ?? abort(404);
});
这适用于我的 get route 测试。下面的测试按预期通过。 ($this->headers 只是我在许多测试所需的 setUp 方法中设置的一些位)
/** @test */
public function error_received_if_note_does_not_exist()
{
$this->withExceptionHandling();
$response = $this->json('GET', '/api/v1/note/1', [], $this->headers);
$response->assertStatus(404);
}
但是这个删除路由失败了……
/**
* @test
* @expectedException \Illuminate\Database\Eloquent\ModelNotFoundException
*/
public function error_received_if_note_not_found()
{
$this->withExceptionHandling();
$response = $this->json('DELETE', '/api/v1/note/1', [], $this->headers);
$response->assertStatus(404);
}
有消息
Failed asserting that exception of type "\Illuminate\Database\Eloquent\ModelNotFoundException" is thrown.
我知道这个异常在技术上是正确的,但我想断言我得到一个 404 错误代码。
这里是 routes/api.php 文件
Route::apiResource('note', 'NoteController')->only([
'show',
'destroy'
]);
我正在拔头发。欢迎任何想法。
【问题讨论】:
标签: php laravel laravel-5 phpunit