【发布时间】:2016-09-12 19:37:41
【问题描述】:
这些是我的模拟:
$this->user->method('getCustomerId')
->willReturn($this->customerId);
$this->userToken->method('getUser')
->willReturn($this->user);
$this->securityContext->method('getToken')
->willReturn($this->userToken);
$this->securityContext->expects($this->once())
->method('isGranted')
->will($this->returnValue(true));
这是我正在测试的类的代码:
$token = $securityContext->getToken();
$isFullyAuthenticated = $securityContext->isGranted('IS_AUTHENTICATED_FULLY');
它抛出错误:
Symfony\Component\Security\Core\Exception\AuthenticationCredentialsNotFoundException: The security context contains no authentication token. One possible reason may be that there is no firewall configured for this URL.
我现在不知道该怎么做,尽管模拟拦截了对方法的调用并返回了我想要的任何内容。但在这种情况下,isGranted 方法似乎没有被模拟
【问题讨论】:
-
不要嘲笑你不拥有的东西。只需创建一个实例并在其中放入一个令牌。