我是学习单元测试,我试图解决了以下问题:ZF2单元测试认证
Zend\ServiceManager\ServiceManager::get was unable to fetch or create an instance for zfcUserAuthentication
...使用在给出的唯一答案:
Simple ZF2 Unit Tests for a controller using ZfcUser
所以我的setUp函数看起来一样。不幸的是,我得到错误信息:
Zend\Mvc\Exception\InvalidPluginException: Plugin of type Mock_ZfcUserAuthentication_868bf824 is invalid; must implement Zend\Mvc\Controller\Plugin\PluginInterface
它的代码(在我的代码拆分以同样的方式),这部分造成的:
$this -> controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock); // Error refers to this line.
的$ authMock对象显然不是实现plugininterface,我需要实现以传入setService。
$ authMock不是意味着在那里传递,因为它用于单元测试吗?我应该使用不同的(面向单元测试)setService方法吗?
我需要一种方法来处理登录到我的应用程序,或者我的单元测试是毫无意义的。
感谢您的任何建议。
===编辑(2013年11月2日)===
我希望把重点放在这一部分澄清,因为我认为这就是问题所在区域:
// Getting mock of authentication object, which is used as a plugin.
$authMock = $this->getMock('ZfcUser\Controller\Plugin\ZfcUserAuthentication');
// Some expectations of the authentication service.
$authMock -> expects($this->any())
-> method('hasIdentity')
-> will($this->returnValue(true));
$authMock -> expects($this->any())
-> method('getIdentity')
-> will($this->returnValue($ZfcUserMock));
// At this point, PluginManager disallows mock being assigned as plugin because
// it will not implement plugin interface, as mentioned.
$this -> controller->getPluginManager()
->setService('zfcUserAuthentication', $authMock);
如果模拟不处理必要的实现,我还有什么假装登录?
我纠正它不是必要的单元测试控制器,因为它是模型?我发现那是我保存我所有的验证码的地方。 – Shoreline 2013-04-04 16:06:49
我最近做了类似的事,没有任何问题。你的完整的测试用例类是什么样的?你的测试引导是什么样的?最后是你要测试的动作。 – Ruben 2013-06-01 01:58:41
单元测试时是否使用特殊的应用程序配置?在这种情况下,zfcUser模块可能不会在测试环境中加载。 – SmasherHell 2013-11-07 14:49:00