TIG*_*GER 3 php phpunit unit-testing
我试图多次调用同一方法时获得不同的返回值。我尝试了很多事情,但没有得到确切的答案。
$mock = $this->getMockBuilder('Test')
->disableOriginalConstructor()
->setMethods(array('testMethod'))
->getMock();
$mock->expects($this->once())->method('testMethod')->will($this->returnValue(true));
$mock->expects($this->second())->method('testMethod')->will($this->returnValue(false));
Run Code Online (Sandbox Code Playgroud)
您可以使用willReturnOnConsecutiveCalls方法
$mock
->expects($this->exactly(2))
->method('testMethod')
->willReturnOnConsecutiveCalls(true, false);
Run Code Online (Sandbox Code Playgroud)
替代(对于phpunit <4):
$mock
->expects($this->exactly(2))
->method('testMethod')
->will($this->onConsecutiveCalls(true, false));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
991 次 |
| 最近记录: |