我伙计们! 当我尝试使用 Laravel 和 Passport 进行一些单元测试时遇到问题。
我有一些路由受 auth:api 中间件保护,但其他一些路由受 Passport scope 中间件保护。
例如,这是我的单元测试之一:
public function testGetPublishedPost(){
$user = factory(\App\User::class)->create();
$this->actingAs($user, 'api');
$article1 = factory(\App\Article::class)->create();
$article2 = factory(\App\Article::class)->create();
$article3 = factory(\App\Article::class)->create( ['published' => false);
$json = $this->call('get', '/api/articles');
$articles = json_decode($json->getContent());
$this->assertEquals(200, $json->getStatusCode());
$this->assertEquals(2, count($articles));
}
一切都工作正常,但现在,我想进行一些测试,包括范围。
我怎样才能做到这一点?
谢谢!
最佳答案
如果您不关心测试身份验证中间件,您可以通过定义函数的临时路径来完全跳过护照身份验证:
Route::get('/_test/api/articles', 'App\Http\Controllers\YourApiController@yourArticleFunction');
//Gain access to Auth:user() in your api controller
$this->be($user);
//Call the temporary route
$response = $this->call('GET', '/_test/api/articles');
关于php - 使用 Laravel Passport 范围进行单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41408808/