如何在 ChefSpec 中 stub_command?
How to stub_command in ChefSpec?
我的食谱中有这个条件:
install_action = (::Win32::Service.exists?(windows_service['name']) ? :configure : :create)
以及规范文件中的 ChefSpec:
#1: not working
allow_any_instance_of(Win32::Service)
.to receive(:exists?)
.with(windows_service[:name])
.and_return(true)
#2: also not working
stub_command("::Win32::Service.exists?(#{windows_service[:name]})").and_return(true)
能否请您帮助找出我在 ChefSpec 测试中遗漏了什么,该测试不起作用并模拟了 return 值。
谢谢
这应该有效:
allow(::Win32::Service).to receive(:exists?).with(windows_service[:name]).and_return(true)
要点是你存根 class 方法 exists?
,而不是实例方法。这就是 allow_any_instance_of
不起作用的原因。 stub_command
实际上用于 shell 命令,例如 stub_command('cat file | grep "hello"')
我的食谱中有这个条件:
install_action = (::Win32::Service.exists?(windows_service['name']) ? :configure : :create)
以及规范文件中的 ChefSpec:
#1: not working
allow_any_instance_of(Win32::Service)
.to receive(:exists?)
.with(windows_service[:name])
.and_return(true)
#2: also not working
stub_command("::Win32::Service.exists?(#{windows_service[:name]})").and_return(true)
能否请您帮助找出我在 ChefSpec 测试中遗漏了什么,该测试不起作用并模拟了 return 值。 谢谢
这应该有效:
allow(::Win32::Service).to receive(:exists?).with(windows_service[:name]).and_return(true)
要点是你存根 class 方法 exists?
,而不是实例方法。这就是 allow_any_instance_of
不起作用的原因。 stub_command
实际上用于 shell 命令,例如 stub_command('cat file | grep "hello"')