我遇到了PHPUnit模擬對象的一個奇怪問題。我有一個應該被調用兩次的方法,所以我使用「at」匹配器。這是第一次調用該方法,但由於某種原因,第二次調用該方法時,我得到「Mocked方法不存在」。我之前使用過「at」匹配器,並且從未遇到過這種情況。
我的代碼看起來是這樣的:
class MyTest extends PHPUnit_Framework_TestCase
{
...
public function testThis()
{
$mock = $this->getMock('MyClass', array('exists', 'another_method', '...'));
$mock->expects($this->at(0))
->method('exists')
->with($this->equalTo('foo'))
->will($this->returnValue(true));
$mock->expects($this->at(1))
->method('exists')
->with($this->equalTo('bar'))
->will($this->returnValue(false));
}
...
}
當我運行測試,我得到:
Expectation failed for method name is equal to <string:exists> when invoked at sequence index 1.
Mocked method does not exist.
如果我刪除了第二匹配,我沒有得到這個錯誤。
有沒有人遇到過這個?
謝謝!
是的,但我認爲這是PHPUnit中的錯誤。該文件說: 返回,當它評估的方法在特定的$ index上被調用匹配的匹配。 – gphilip 2011-05-11 14:35:27
同意,再加上它會更容易,更有效的間諜方法調用如果在()指數將在每個方法的基礎上遞增。 – 2011-05-31 12:33:56
看起來像任何錯誤的預期使用將導致「模擬方法不存在」的消息。很高興知道。 – 2012-10-02 00:17:21