2017-04-16 99 views
0

的方法我想嘲弄一个类的方法返回的特定值的返回值:模拟使用PHPUnit

$this->mock 
    ->method('method') 
    ->willReturn($this->returnValue('foo')) 
; 

这种方法将在内部通过,这将使用它的结果的另一种方法调用。该方法实际上被模拟,但它返回这个:

class PHPUnit_Framework_MockObject_Stub_Return#471 (1) { 
    protected $value => 
    string(3) "foo" 
} 

而不是一个字符串。内部使用此方法的代码会中断。

如何让一个模拟的返回一个真正的字符串,而不是这个对象?

我试过returnCallback但它返回一些更奇怪的......

PHPUnit v5.7.19

PHP v7.1.4

回答

0

我用PHPUnit API错误。它应如下所示:

$this->mock 
    ->method('method') 
    ->will($this->returnValue('foo')) 
; 

->will代替->willReturn