来自测试方法的一段代码:
$mock->expects($this->any())
->method('get')
->willReturn($this->returnValueMap(['foo', new FooMock()]));
$service->doSomething($mock);
返回值mock:
class FooMock {
public $bar;
public function __construct() {
$this->bar = new \stdClass;
}
}
我正在测试的代码:
class Service {
function doSomething($object) {
//do something
return $object->get('foo')->bar;
}
}
错误:
未定义的属性: PHPUnit_Framework_MockObject_Stub_ReturnValueMap :: $杆
答案 0 :(得分:0)
在您的代码中:
$mock->expects($this->any())
->method('get')
->willReturn($this->returnValueMap(['foo', new FooMock()]));
使用参数 - 数组returnValueMap
调用['foo', new FooMock()]
。但是,该方法需要带有映射的数组(documentation)。试试这个:
$mock->expects($this->any())
->method('get')
->willReturn($this->returnValueMap([['foo', new FooMock()]]));