首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在ZfcUser 2和ZfcUser上使用zfcUserIdentity助手测试操作

如何在ZfcUser 2和ZfcUser上使用zfcUserIdentity助手测试操作
EN

Stack Overflow用户
提问于 2014-11-13 01:33:42
回答 2查看 313关注 0票数 0

按照这种方式,Zend 2应用程序的单元测试使用ZfcUser,并尝试在我的控制器中测试一些操作。但是当测试被执行时,我得到和错误,因为我使用$this->zfcUserIdentity()来获取我的视图中的用户名。

我怎样才能做这个测试?我想嘲笑这个方法,但我不知道怎么做。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-11-13 06:28:09

您可以查看ZfcUser测试:https://github.com/ZF-Commons/ZfcUser/blob/master/tests/ZfcUserTest/Controller/UserControllerTest.php#L61-L279

简单地说,您必须模拟ZfcUserIdentity控制器插件,并告诉控制器插件管理器使用该模拟。

票数 0
EN

Stack Overflow用户

发布于 2015-03-24 01:05:58

我在我的AbstractHttpControllerTestCase里用了这样的东西

代码语言:javascript
复制
/**
 * @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);
}

然后在我的测试中:

代码语言:javascript
复制
$this->login(['admin']);
$this->dispatch($url);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26899863

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档