如何在 phpunit 中添加授权 header ?我正在测试一个需要 api_token 的 json api。 laravel 文档提供了一个 actAs 方法。但这在我的情况下不起作用,因为 api token 与用户表没有直接关系。
编辑:
public function test_returns_response_with_valid_request()
{
$response = $this->json('post', '/api/lookup', [
'email' => 'user@gmail.com'
]);
$response->assertStatus(200);
$response->assertJsonStructure([
'info' => [
'name'
]
]);
}
最佳答案
根据 documentation .
You may also specify which guard should be used to authenticate the given user by passing the guard name as the second argument to the
actingAs
method:$this->actingAs($user, 'api');
关于使用 api token 身份验证的 laravel phpunit 测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46769959/