在纯PHPUnit模拟中,我可以这样做:
$mock->expects($this->at(0))
->method('isReady')
->will($this->returnValue(false));
$mock->expects($this->at(1))
->method('isReady')
->will($this->returnValue(true));我不能用预言做同样的事。有可能吗?
发布于 2015-09-24 13:14:26
您可以使用:
$mock->isReady()->willReturn(false, true);显然,它没有文档化(请参阅https://gist.github.com/gquemener/292e7c5a4bbb72fd48a8)。
发布于 2016-08-27 12:26:30
还有另一种有记录的方法可以做到这一点。如果您期望在第二个调用中得到不同的结果,这意味着在这两者之间发生了一些变化,并且您可能使用setter来修改对象的状态。这样,您就可以告诉模拟在调用带有特定参数的setter之后返回特定的结果。
$mock->isReady()->willReturn(false);
$mock->setIsReady(true)->will(function () {
$this->isReady()->willReturn(true);
});
// OR
$mock->setIsReady(Argument::type('boolean'))->will(function ($args) {
$this->isReady()->willReturn($args[0]);
});这里有更多关于https://github.com/phpspec/prophecy#method-prophecies-idempotency的内容。
https://stackoverflow.com/questions/32336511
复制相似问题