模拟 Laravel 配置缓存
Mocking Laravel configuration cache
我正在编写一个新的 artisan 命令,如果配置被缓存(通过 config:cache
),我不想 运行:
class NewCommand extends Command
{
public function handle()
{
if (app()->configurationIsCached())
{
$this->error('Configuration is cached. Unable to run command');
return 1;
}
}
}
我正在尝试编写一个单元测试来解决这个问题,但是失败了:
public function test_command_not_run_if_config_cached()
{
App::shouldReceive('configurationIsCached')
->once()
->andReturn(true);
$this->artisan('new:command')
->expectsOutput('Configuration is cached. Unable to run command');
->assertExitCode(1);
}
结果:Method configurationIsCached() from Mockery_0_Illuminate_Foundation_Application should be called exactly 1 times but called 0 times
是否有另一种方法可以模拟配置缓存在单元测试中?
原来 configurationIsCached()
在 App
外观上可用。目前没有记录,但将命令中的句柄函数更新为
public function handle()
{
if (App::configurationIsCached())
{
$this->error('Configuration is cached. Unable to run command');
return 1;
}
}
允许测试通过。感谢@Namosheck 指出这一点
我正在编写一个新的 artisan 命令,如果配置被缓存(通过 config:cache
),我不想 运行:
class NewCommand extends Command
{
public function handle()
{
if (app()->configurationIsCached())
{
$this->error('Configuration is cached. Unable to run command');
return 1;
}
}
}
我正在尝试编写一个单元测试来解决这个问题,但是失败了:
public function test_command_not_run_if_config_cached()
{
App::shouldReceive('configurationIsCached')
->once()
->andReturn(true);
$this->artisan('new:command')
->expectsOutput('Configuration is cached. Unable to run command');
->assertExitCode(1);
}
结果:Method configurationIsCached() from Mockery_0_Illuminate_Foundation_Application should be called exactly 1 times but called 0 times
是否有另一种方法可以模拟配置缓存在单元测试中?
原来 configurationIsCached()
在 App
外观上可用。目前没有记录,但将命令中的句柄函数更新为
public function handle()
{
if (App::configurationIsCached())
{
$this->error('Configuration is cached. Unable to run command');
return 1;
}
}
允许测试通过。感谢@Namosheck 指出这一点