按照这种方式,Zend 2应用程序的单元测试使用ZfcUser,并尝试在我的控制器中测试一些操作。但是当测试被执行时,我得到和错误,因为我使用$this->zfcUserIdentity()来获取我的视图中的用户名。
我怎样才能做这个测试?我想嘲笑这个方法,但我不知道怎么做。
发布于 2014-11-13 06:28:09
您可以查看ZfcUser测试:https://github.com/ZF-Commons/ZfcUser/blob/master/tests/ZfcUserTest/Controller/UserControllerTest.php#L61-L279
简单地说,您必须模拟ZfcUserIdentity控制器插件,并告诉控制器插件管理器使用该模拟。
发布于 2015-03-24 01:05:58
我在我的AbstractHttpControllerTestCase里用了这样的东西
/**
* @param array $roles e.g. ['admin']
*/
protected function login($roles) {
$userMock = $this->getMock('Application\Entity\User', [], [], '', false);
$userMock->expects($this->any())
->method('getRoles')->will($this->returnValue($roles));
$storageMock = $this->getMock('\Zend\Authentication\Storage\NonPersistent');
$storageMock->expects($this->any())
->method('isEmpty')->will($this->returnValue(false));
$storageMock->expects($this->any())
->method('read')->will($this->returnValue($userMock));
$sm = $this->getApplicationServiceLocator();
$auth = $sm->get('Zend\Authentication\AuthenticationService');
$auth->setStorage($storageMock);
}然后在我的测试中:
$this->login(['admin']);
$this->dispatch($url);https://stackoverflow.com/questions/26899863
复制相似问题