微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用PHPUnit在doctrine2中模拟findOneBy“field”

如果我模拟存储库方法找到我得到预期的结果,
但如果我调用findBy,findOneBy,findOneById我总是得到null.

代码示例:

$mock->expects($this->once())
                ->method('getId')
                ->will($this->returnValue(1));
$mockRepository->expects($this->any())
                         ->method('findBy') //if here I use 'find' works for all other cases always null
                         ->will($this->returnValue($mock));

有这种情况发生的原因吗?
有可能像findById或findOneById一样嘲笑Doctrine2的“魔法”方法吗?
如果是的话,我的方法出了什么问题?

解决方法:

评论中所述,可以通过模拟魔术方法调用来实现.例如:

 // Mocked return value
 $mock->expects($this->once())
            ->method('getId')
            ->will($this->returnValue(1));

 // Example of repo mocking
 // $mockRepository= $this->getMock('Doctrine\ORM\EntityRepository', array(), array(), '', false);

$this->mockRepository
->expects($this->at(1))
->method('__call')
->with('findBy')
->will($this->returnValue($mock));

希望这有帮助

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐