在 artisan 命令中使用的接口上使用链式部分模拟
Using chained partial mocks on an interface used in an artisan command
我正在尝试对 Laravel 5.3 中的 artisan 命令进行单元测试。该命令调用作为接口提供给命令构造函数的 class 中的函数。该接口调用另一个 class 中的函数。这是一般设置。
class MyCommand
{
public function __construct(MyRepositoryInterface $interface)
{
...
$this->interface = $interface;
...
}
public function fire()
{
$this->interface->useTheSecondClass();
}
}
class MyRepository implements MyRepositoryInterface
{
public function __construct(MySecondRepositoryInterface $second)
{
...
$this->second = $second;
...
}
public function useTheSecondClass()
{
$response = $this->second->getSomeValue();
}
}
class MySecondRepository implements MySecondRepositoryInterface
{
/**
* @return Some\External\Client
*/
public function getExternalClient()
{
....
return $external_client;
}
public function getSomeValue()
{
$client = $this->getExternalClient();
$something = $client->doSomething();
Event::fire('some event based on $something`);
return $something;
}
}
我正在尝试模拟 MySecondRepository -> getExternalClient()
中返回的变量,以便我可以伪造外部 API 调用并使用伪造的数据来测试 MySecondRepository -> getSomeValue()
和 MyRepository -> useTheSecondClass()
从 MyCommand
class 调用的功能。
public function testMyCommand()
{
$external_client_mock = Mockery::mock("Some\External\Client");
$external_client_mock->shouldReceive("doSomething")
->andReturn("some values");
$second_repository_mock = Mockery::mock("MySecondRepositoryInterface")
->makePartial();
$second_repository_mock->shouldReceive("getExternalClient")
->andReturn($external_client_mock);
$resource = new MyRepository($second_repository_mock);
$this->app->instance("MyRepositoryInterface", $resource);
$class = App::make(MyCommand::class);
$class->fire();
...
}
我已经成功地使用了这个完全相同的模拟链来直接测试 $resource
变量(例如,直接测试 $resource->useTheSecondClass()
,而不是通过 MyCommand
),但是在这种情况下,虽然$second_repository_mock->getExternalClient()
是正确的模拟,测试仍然期望对 $second_repository_mock->getSomeValue()
有一个模拟期望。由于 $second_repository_mock
设置为部分模拟,我不明白为什么它仍在寻找要模拟的所有函数。
如果我删除 $external_client_mock
部分并完全模拟 $second_repository_mock
我的测试与 artisan 命令工作直接相关,但是我想测试事件是否在 getSomeValue()
中触发从 artisan 命令中正确处理,如果我不能使用部分模拟,我就不能这样做。
有没有人知道为什么这不起作用?
您正在尝试模拟一个毫无意义的界面。接口没有具体的实现可以使用。这导致错误的代码。只需模拟您的存储库,它就会起作用。
编辑
只需 运行 具有以下重构的测试,它们就变成了绿色:
public function testMyCommand() // phpcs:ignore
{
$external_client_mock = \Mockery::mock("App\Client");
$external_client_mock->shouldReceive("doSomething")
->andReturn("some values");
$second_repository_mock = \Mockery::mock("App\MySecondRepository[getExternalClient]")
->shouldIgnoreMissing();
$second_repository_mock->shouldReceive("getExternalClient")
->andReturn($external_client_mock);
$resource = new MyRepository($second_repository_mock);
$this->app->instance("App\MyRepositoryInterface", $resource);
$class = \App::make(\App\MyClass::class);
$class->fire();
}
唯一的主要区别是倒数第二行有 App\MyCommand
而不是 App\MyClass
。
我正在尝试对 Laravel 5.3 中的 artisan 命令进行单元测试。该命令调用作为接口提供给命令构造函数的 class 中的函数。该接口调用另一个 class 中的函数。这是一般设置。
class MyCommand
{
public function __construct(MyRepositoryInterface $interface)
{
...
$this->interface = $interface;
...
}
public function fire()
{
$this->interface->useTheSecondClass();
}
}
class MyRepository implements MyRepositoryInterface
{
public function __construct(MySecondRepositoryInterface $second)
{
...
$this->second = $second;
...
}
public function useTheSecondClass()
{
$response = $this->second->getSomeValue();
}
}
class MySecondRepository implements MySecondRepositoryInterface
{
/**
* @return Some\External\Client
*/
public function getExternalClient()
{
....
return $external_client;
}
public function getSomeValue()
{
$client = $this->getExternalClient();
$something = $client->doSomething();
Event::fire('some event based on $something`);
return $something;
}
}
我正在尝试模拟 MySecondRepository -> getExternalClient()
中返回的变量,以便我可以伪造外部 API 调用并使用伪造的数据来测试 MySecondRepository -> getSomeValue()
和 MyRepository -> useTheSecondClass()
从 MyCommand
class 调用的功能。
public function testMyCommand()
{
$external_client_mock = Mockery::mock("Some\External\Client");
$external_client_mock->shouldReceive("doSomething")
->andReturn("some values");
$second_repository_mock = Mockery::mock("MySecondRepositoryInterface")
->makePartial();
$second_repository_mock->shouldReceive("getExternalClient")
->andReturn($external_client_mock);
$resource = new MyRepository($second_repository_mock);
$this->app->instance("MyRepositoryInterface", $resource);
$class = App::make(MyCommand::class);
$class->fire();
...
}
我已经成功地使用了这个完全相同的模拟链来直接测试 $resource
变量(例如,直接测试 $resource->useTheSecondClass()
,而不是通过 MyCommand
),但是在这种情况下,虽然$second_repository_mock->getExternalClient()
是正确的模拟,测试仍然期望对 $second_repository_mock->getSomeValue()
有一个模拟期望。由于 $second_repository_mock
设置为部分模拟,我不明白为什么它仍在寻找要模拟的所有函数。
如果我删除 $external_client_mock
部分并完全模拟 $second_repository_mock
我的测试与 artisan 命令工作直接相关,但是我想测试事件是否在 getSomeValue()
中触发从 artisan 命令中正确处理,如果我不能使用部分模拟,我就不能这样做。
有没有人知道为什么这不起作用?
您正在尝试模拟一个毫无意义的界面。接口没有具体的实现可以使用。这导致错误的代码。只需模拟您的存储库,它就会起作用。
编辑
只需 运行 具有以下重构的测试,它们就变成了绿色:
public function testMyCommand() // phpcs:ignore
{
$external_client_mock = \Mockery::mock("App\Client");
$external_client_mock->shouldReceive("doSomething")
->andReturn("some values");
$second_repository_mock = \Mockery::mock("App\MySecondRepository[getExternalClient]")
->shouldIgnoreMissing();
$second_repository_mock->shouldReceive("getExternalClient")
->andReturn($external_client_mock);
$resource = new MyRepository($second_repository_mock);
$this->app->instance("App\MyRepositoryInterface", $resource);
$class = \App::make(\App\MyClass::class);
$class->fire();
}
唯一的主要区别是倒数第二行有 App\MyCommand
而不是 App\MyClass
。