使用 Laravel Passport / Phpunit 验证我的 API
Authenticating my APIs with Laravel Passport / Phpunit
我刚刚根据 docs.
验证了我的 API
现在的情况是,我使用 phpunit 进行的功能测试失败了。我收到 "Unauthenticated" 错误。
我试过使用一个中间件,推荐here,但是没用。
我想我需要在请求中包含 csrf 字段,但我真的不知道如何进行测试
这是我的测试:
$this->json('POST', '/api/v1/category/create',$data)
->seeJson($data)
->seeInDatabase('category', $data);
知道我该怎么做吗???
您可以使用 Illuminate\Foundation\Testing\WithoutMiddleware
特征,它会绕过身份验证中间件(但也绕过所有其他中间件)。
不过,更具弹性的方法是使用 the built-in testing handling 进行身份验证。通过 actingAs
函数,有效用户将执行您的测试,就好像该用户已登录一样。
$user = factory(App\User::class)->create();
$this->actingAs($user)->visit('/');
我刚刚根据 docs.
验证了我的 API现在的情况是,我使用 phpunit 进行的功能测试失败了。我收到 "Unauthenticated" 错误。
我试过使用一个中间件,推荐here,但是没用。
我想我需要在请求中包含 csrf 字段,但我真的不知道如何进行测试
这是我的测试:
$this->json('POST', '/api/v1/category/create',$data)
->seeJson($data)
->seeInDatabase('category', $data);
知道我该怎么做吗???
您可以使用 Illuminate\Foundation\Testing\WithoutMiddleware
特征,它会绕过身份验证中间件(但也绕过所有其他中间件)。
不过,更具弹性的方法是使用 the built-in testing handling 进行身份验证。通过 actingAs
函数,有效用户将执行您的测试,就好像该用户已登录一样。
$user = factory(App\User::class)->create();
$this->actingAs($user)->visit('/');