我是单元测试的菜鸟,我想测试我的 API。因此,当我尝试在 /login_check 路径和 CRUL 命令行中访问时,LexikJWTAuthenticationBundle 工作正常。但是当我尝试做 TestCase 时,我得到了这个错误:
1) Tests\AppBundle\Controller\StoreControllerTest::testPOSTRate GuzzleHttp\Exception\ClientException: 客户端错误: GET http://localhost:8000/api/stores导致 401 Unauthorized response: {"code":401,"message" :"无效的 JWT 令牌"}
我的 testPOST 函数:
public function testPOSTRate(){
$data = array(
'rate' => 1,
'store' => 1
);
// I'm not sure if it's the right way to get Token
$token = $this->getService('lexik_jwt_authentication.encoder')->encode([
'username' => 'Nacer',
]);
$this->client->get('http://localhost:8000/api/stores', [
'body' => json_encode($data),
'headers' => [
'Authorization' => 'Bearer '.$token
]
]);
}