PHPUnit mock with multiple expects() calls

筅森魡賤 提交于 2019-12-01 06:33:49

If you know, that method is called once use $this->once() in expects(), otherwise use $this->any()

$mock = $this->getMock('nameOfTheClass', array('firstMethod','secondMethod','thirdMethod'));
$mock->expects($this->once())
     ->method('firstMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('secondMethod')
     ->will($this->returnValue('value'));
$mock->expects($this->once())
     ->method('thirdMethod')
     ->will($this->returnValue('value'));
FMaz008

I've tried this, and it seems to works as long as the call order stays good:

$mock = $this->getMock('mockWorker', array('display', 'process'));
$mock->expects($this->exactly(1))
     ->method('display')
     ->will($this->returnValue(null));
$mock->expects($this->exactly(1))
     ->method('process');
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!