依赖中的嘲弄和方法链

时间:2020-12-30 14:23:59

标签: php laravel tdd mockery

我对测试很陌生,尤其是对 Mockery。

在 Laravel 项目中,我有一个包装外部 api 库的类。像这样:

class ApiWrapper {

  private $api;

  public function __construct(ExternalApiLibrary $api)
  {
    $this->api = $api;
  }

  public function method()
  {
    $this->api->method1('Foo')
              ->method2('Bar')
              ->method3('Not');
  }

}

在我的测试类中,我使用 mockery 来模拟 ExternalApiLibrary,我想测试这 3 个方法是否使用它们的参数调用。

public function test_that_works()
{
  $this->mock(ExternalApiLibrary::class, function (MockInterface $mock) {
    $mock
      ->shouldReceive('method1')
      ->with('Foo');

    $mock
      ->shouldReceive('method2')
      ->with('Bar');

    $mock
      ->shouldReceive('method3')
      ->with('Not');
  });

  $apiWrapper = $this->app->make(ApiWrapper::class);
  $apiWrapper->method();
}

但我明白了 Method Mockery_0_ExternalApiLibrary::method2() does not exist on this mock object

我明白原因,但我如何测试这些方法是否在该外部库上被调用?

0 个答案:

没有答案