我开始使用ZfcUser + BjyAuthorize为Zend Framework2项目进行单元测试。
我不是单元测试专家,所以我找到了如何模拟ZfcUser here。
现在我也不得不嘲笑BjyAuthorize了。以前有没有人成功做到过?
发布于 2013-10-27 04:54:19
好吧,现在对此做出回应肯定为时已晚。
我找到了一个使用模拟对象的解决方案(我不喜欢使用模拟...),我找不到一种正确初始化BjyAuthorize的方法……
我的测试控制器是从AbstractHttpControllerTestCase扩展而来的
在testController::setUp()中,我创建了这样的模拟对象:
// Creating mock
$mockBjy = $this->getMock("BjyAuthorize\Service\Authorize", array("isAllowed"), array($this->getApplicationConfig(), $this->getApplication()->getServiceManager()));
// Bypass auth, force true
$mockBjy->expects($this->any())
->method('isAllowed')
->will($this->returnValue(true));
// Overriding BjyAuthorize\Service\Authorize service
$this->getApplication()
->getServiceManager()
->setAllowOverride(true)
->setService('BjyAuthorize\Service\Authorize', $mockBjy);虽然我不喜欢这个解决方案,但我觉得它真的很难看,但我找不到其他的方法。
https://stackoverflow.com/questions/18466934
复制相似问题