开发者

PHPUnit mock with multiple expects() calls

开发者 https://www.devze.com 2023-03-01 19:21 出处:网络
Using PHPUnit, I wonder how we can have multiple expectation from the same stub/mock. For example, I want to test that the mock will have开发者_如何学Go the method display() called and return NULL. I

Using PHPUnit, I wonder how we can have multiple expectation from the same stub/mock.

For example, I want to test that the mock will have开发者_如何学Go the method display() called and return NULL. I also want to test that the method process() will be called.

In fact my test is called testProcessIsCalledIfDisplayReturnNull().

So I need to setup 2 expectations on the same mock object, and the manual doesn't really help about that :(


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'));


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');
0

精彩评论

暂无评论...
验证码 换一张
取 消