我有一个 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"'));
但是,如果没有使用参数调用模拟,这会导致 PHPUnit 抱怨'two'
,所以我假设 的定义methodToMock('two')
覆盖了第一个的定义。
所以我的问题是:有没有办法让 PHPUnit 模拟对象根据其参数返回不同的值?如果是这样,怎么办?