我正在为我正在处理的项目编写一些单元测试,但我似乎无法测试作为登录用户发布。
我尝试将持有者令牌添加到 env 文件,这在我的机器上有效,但在推送后运行测试的伙伴失败。
public function test_orders_route_authenticated_user () {
$data =
'{
"orderID": "001241",
"sku": "123456",
"quantity": 9,
"pricePerUnit": 78,
"priceTotal": 702
}';
$this->user = factory(User::class)->create();;
//dd($user);
$response = $this->withHeaders([
'Authorization'=>'MPBtsJN5qf',
])->json('POST', 'api/products',[
$data
]);
$response->assertStatus(200);
}
所以这段代码给了我错误 500。我设法得到错误 401 和 500,但从未达到预期的状态 200。请帮忙
如护照文档中所述,您可以使用Passport::actingAs(...)
:
public function test_orders_route_authenticated_user()
{
Passport::actingAs(
factory(User::class)->create()
);
$data = [
'orderID' => '001241',
'sku' => '123456',
'quantity' => 9,
'pricePerUnit' => 78,
'priceTotal' => 702,
];
$this->json('post', 'api/products', $data)->assertStatus(200);
}
您应该在路由上应用 jwt 身份验证,例如:
Route::group(['middleware' => 'jwt.auth'], function () {
Route::get('/index','IndexController@index');
});