简单 Laravel 护照路线测试

Simple Laravel Passeport Route Testing

我在对默认 Passport 5.8 路由执行单元测试时遇到一个小问题。

事实上我在get模式下测试了route/oauth/clients:

/** @test */
   public function getOauthClients()
   {
       $user = factory(User::class)->make();
       $response = $this->actingAs($user)->getJson('/oauth/clients');
       $response->assertSuccessful();
   }

但是当我想测试get模式下默认提供的路由:/oauth/token时,我不知道需要遵循什么步骤。

提前谢谢你。

你应该试试:

Passport::actingAs(
    factory(User::class)->create()
);
$response = $this->getJson('/oauth/clients');
// ...

为此,Passport 附带了一些测试助手,例如上面的 actingAs 方法。

引用自documentation

Passport's actingAs method may be used to specify the currently authenticated user as well as its scopes. The first argument given to the actingAs method is the user instance and the second is an array of scopes that should be granted to the user's token: