测试未登录用户回复跳转到登录界面,测试结果为 404?
test 文件:
public function unauthenticated_user_may_no_add_replies()
{
$this->withExceptionHandling()->post('/threads/1/replies', [])
->assertRedirect('/login');
}
web 路由文件:
Route::post('/threads/{thread}/replies','RepliesController@store')->name('reply.store');
Controller :
public function _construct()
{
$this->middleware('auth');
}
public function store(Thread $thread)
{
$thread->addReply([
'body' => request('body'),
'user_id' => auth()->id(),
]);
return redirect(route('threads.show',[$thread->channel,$thread->id]));
}
测试结果:

找了半天不知道为什么是404 呢?
页面中测试没有问题
2、id 为1 的thread 模型是存在的,

关于 LearnKu
这里之所以是404 的问题,是因为还没有创建 thread,所以并未存在 id 为1的模型实例,所以路由
是不存在的, 所以我们需要先创建thread 实例