我编写了两个PHPUnit测试,仅当用户通过身份验证时,该测试才更新和删除书记录。我正在使用Passport进行身份验证,但是两个测试均失败,并显示403
错误。我下面的代码可能有什么问题
public function test_onlyAuthenticatedUserCanUpdateBookSuccessfully()
{
$user = factory(User::class)->create();
Passport::actingAs($user);
$book = factory(Book::class)->create();
$response = $this->json('PUT', '/api/books/'.$book->id, [
'id' => $book->id,
'title' => 'Updated book title',
'author'=> 'New Guy'
]);
$response->assertStatus(201);
}
public function test_onlyAuthenticatedUserCanDeleteBook()
{
$user = factory(User::class)->create();
Passport::actingAs($user);
$book = factory(Book::class)->create();
$response = $this->json('DELETE', '/api/books/'.$book->id);
$response->assertStatus(204);
}
这是路由文件夹中的api.php文件
Route::apiResource('books', 'BookController');