【发布时间】:2010-09-21 15:09:29
【问题描述】:
我有一个 PHPUnit 模拟对象,无论它的参数是什么,它都会返回 'return value':
// From inside a test...
$mock = $this->getMock('myObject', 'methodToMock');
$mock->expects($this->any))
->method('methodToMock')
->will($this->returnValue('return value'));
我希望能够根据传递给模拟方法的参数返回不同的值。我尝试过类似的方法:
$mock = $this->getMock('myObject', 'methodToMock');
// methodToMock('one')
$mock->expects($this->any))
->method('methodToMock')
->with($this->equalTo('one'))
->will($this->returnValue('method called with argument "one"'));
// methodToMock('two')
$mock->expects($this->any))
->method('methodToMock')
->with($this->equalTo('two'))
->will($this->returnValue('method called with argument "two"'));
但是如果没有使用参数'two' 调用模拟,这会导致PHPUnit 抱怨,所以我假设methodToMock('two') 的定义会覆盖第一个的定义。
所以我的问题是:有没有办法让 PHPUnit 模拟对象根据其参数返回不同的值?如果有,怎么做?
【问题讨论】:
标签: php unit-testing mocking phpunit