gpt4 book ai didi

php - Laravel PHPUnit 测试抛出禁止的错误

转载 作者:行者123 更新时间:2023-12-05 07:29:49 26 4
gpt4 key购买 nike

我编写了两个 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);
}

这是 routes 文件夹中的 api.php 文件

Route::apiResource('books', 'BookController');

最佳答案

尝试更改表单请求授权

关于php - Laravel PHPUnit 测试抛出禁止的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52558700/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com