【发布时间】:2011-09-10 12:11:53
【问题描述】:
使用 PHPUnit,我正在使用 ->at() 测试一系列方法调用,如下所示:
$mock->expects($this->at(0))->method('execute')->will($this->returnValue('foo'));
$mock->expects($this->at(1))->method('execute')->will($this->returnValue('bar'));
$mock->expects($this->at(2))->method('execute')->will($this->returnValue('baz'));
如何设置模拟,以便在上述场景中,if execute() 被调用四次或更多次,它会立即失败?我试过这个:
$mock->expects($this->at(3))->method('execute')->will($this->throwException(new Exception('Called too many times.')));
但是如果 execute() 被 not 调用四次,这也会失败。它需要立即失败,否则被测系统会产生自己的错误,导致产生的错误信息不清楚。
【问题讨论】:
标签: php unit-testing mocking phpunit