错误写法

$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 实例。

 

相关文章:

  • 2021-12-11
  • 2022-02-23
  • 2022-12-23
  • 2021-08-29
  • 2022-12-23
  • 2021-06-08
  • 2021-10-02
  • 2022-03-10
猜你喜欢
  • 2022-01-07
  • 2022-12-23
  • 2021-12-04
  • 2022-12-23
  • 2022-01-27
  • 2022-12-23
  • 2021-05-14
相关资源
相似解决方案