如何在PHPUnit_Framework_MockObject_Stub_ReturnValueMap对象上设置属性?

时间:2016-10-03 10:11:30

标签: php unit-testing mocking phpunit

来自测试方法的一段代码:

$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 :: $杆

1 个答案:

答案 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()]]));