我正在尝试使用 this method described by the author of PHPUnit 模拟单例并 stub 其中一种方法:
public function setUp() {
$this->_foo = $this->getMockBuilder('Foo')
->disableOriginalConstructor()
->getMock();
$this->_foo->expects($this->any())
->method('bar')
->will($this->returnValue('bar'));
var_dump($this->_foo->bar());
}
问题是每次都会转储 NULL
。据我了解,当您模拟一个对象时,所有方法都将替换为返回 NULL
的 stub ,除非像我正在做的那样明确 stub 。那么,由于我已经对 bar()
方法进行了 stub ,为什么它不转储预期的 'bar'
字符串?我做错了什么?
最佳答案
我遇到了同样的问题,对我来说,问题原来是我调用的方法在原始对象上不存在,而是由 __call
处理的。解决方案最终看起来像:
$this->_foo->expects($this->any())
->method('__call')
->with($this->equalTo('bar'))
->will($this->returnValue('bar'));
关于返回 NULL 的 PHPUnit stub 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13241809/