【发布时间】:2019-07-23 15:02:29
【问题描述】:
我正在尝试测试以下方法:
/* ConfigurationService.php*/
public function checkConfigs()
{
$configurations = $this->getConfigurations();
return $configurations['configExample'] === '1';
}
考虑到getConfigurations() 方法调用了这个文件之外的其他方法,在ConfigurationRepository.php 内部,我试图只模拟它的返回并执行我想要测试的方法(checkConfigs()):(省略了一些代码)
/* ConfigurationServiceTest.php */
$configurationRepoMock = \Mockery::mock(ConfigurationRepository::class);
$configurationRepoMock
->shouldReceive('getConfigurations')
->once()
->andReturn(['configExample' => '1']);
$configurationServiceMock = \Mockery::mock(ConfigurationService::class);
$this->app->instance('App\Services\ConfigurationService', $configurationServiceMock);
$configurationServiceInstance = new ConfigurationService($configurationRepoMock);
$response = $configService->checkConfigs();
问题是,不是返回模拟结果(['configExample' => '1']),而是方法getConfigurations()执行,由于其中的其他方法调用而失败,返回错误:
Mockery\Exception\BadMethodCallException:收到 Mockery_1_App_Repositories_API_ConfigurationRepository::methodInsideGetConfigurations(),但未指定预期
总结一下,andReturn() 不起作用。有什么想法吗?
【问题讨论】:
-
getConfigurations看起来是ConfigurationService的一部分,而不是您嘲笑的ConfigurationRepository。这个方法有什么作用? -
它是
ConfigurationService的一部分,但它从ConfigurationRepository进行方法调用,然后根据这些方法返回数据 -
当你用这个调用替换容器中的实例时:
$this->app->instance('App\Services\ConfigurationService', $configurationServiceMock);并因此使用new关键字来创建一个类,你不会替换任何东西。您必须使用app(ConfigurationService::class)从容器中解析实例才能获取模拟实例。
标签: php testing mocking phpunit mockery