嘲笑 – PHPUnit模拟多个期望
发布时间:2020-12-13 16:15:57 所属栏目:PHP教程 来源:网络整理
导读:来自谷歌模拟的背景,我很惊讶这不起作用,除非我做错了. 我只想确保从不使用特定的类类型调用方法,但可以为其他类类型调用.所以这里有我解释我想要的代码: $this-entityManagerMock -expects($this-any()) -method('persist');$this-entityManagerMock -expe
来自谷歌模拟的背景,我很惊讶这不起作用,除非我做错了.
我只想确保从不使用特定的类类型调用方法,但可以为其他类类型调用.所以这里有我解释我想要的代码: $this->entityManagerMock ->expects($this->any()) ->method('persist'); $this->entityManagerMock ->expects($this->never()) ->method('persist') ->with($this->isInstanceOf('MySpecificClass')); 现在我收到类似这样的消息: DoctrineORMEntityManager::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); })); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |