嘲笑 – PHPUnit模拟多个期望

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了嘲笑 – PHPUnit模拟多个期望脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
来自谷歌模拟的背景,我很惊讶这不起作用,除非我做错了.

我只想确保从不使用特定的类类型调用方法,但可以为其他类类型调用.所以这里有我解释我想要的代码

$this->entITyManagerMock
      ->expects($this->any())
      ->;method('PErsist');
$this->entityManagerMock
     ->expects($this->never())
     ->method('persist')
     ->with($this->isinstanceOf('MySpecificClass'));

现在我收到类似这样的消息:

Doctrine\ORM\EntityManager::persist(DifferentClassType Object (...)) was not expected to be called.

当我期望第一次期望处理它.

我尝试了这个,但结果是一样的:

$this->entityManagerMock
      ->expects($this->any())
      ->method('persist')
      ->with($this->anything());
$this->entityManagerMock
     ->expects($this->never())
     ->method('persist')
     ->with($this->isinstanceOf('MySpecificClass'));

这是我第一次在PHPUnit中使用模拟,但在我看来,它已被破坏和/或没用.我知道现在大多数Web开发人员都使用TDD,所以必须有更好的方法来实现这一点.

解决方法

作为解决方案,您可以使用 returnCallback

$this->entityManagerMock
     ->expects($this->any())
     ->method('persist')
     ->will($this->returnCallback(function ($object) {
         self::assertnotinstanceOf('MySpecificClass',$object);
     }));

脚本宝典总结

以上是脚本宝典为你收集整理的嘲笑 – PHPUnit模拟多个期望全部内容,希望文章能够帮你解决嘲笑 – PHPUnit模拟多个期望所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。