php Mockery 错误 "call_user_func_array() expects parameter 1 to be a valid callback, class 'Mockery\Expectation' does not have a method"

错误写法

$mock = Mockery::mock(MyClass::class)
    ->shouldReceive('foo')
    ->once()
    ->with($arg)
    ->andReturn($returnValue);

 

Mockery::mock(MyClass::class) 返回的是 \Mockery\MockInterface

 

而后面的几个方法都是 \Mockery\Expectation 里面的方法。

 

最后我们调用 mock 实例的方法时需要的是 \Mockery\MockInterface,而不是  \Mockery\Expectation , 所以正确的写法如下:

$mock = Mockery::mock(MyClass::class);
$mock->shouldReceive('foo')
    ->once()
    ->with($arg)
    ->andReturn($returnValue);
var_dump($mock->foo(3) === 5);

 

又或者在第一种写法后面 $mock->getMock()->foo(3) 这样获取 mock 实例。

 

posted @ 2019-02-25 13:27  佚名000  阅读(3766)  评论(0编辑  收藏  举报