假设,我有以下食谱:
install_iis:
require 'chef/win32/version'
windows_version = Chef::ReservedNames::Win32::Version.new
node.set['iis']['components'] = [
'IIS-HttpErrors',
'IIS-HttpRedirect',
'IIS-HttpLogging',
'IIS-LoggingLibraries',
'IIS-RequestMonitor',
'WAS-WindowsActivationService',
'WAS-ProcessModel',
'IIS-StaticContent',
'IIS-DefaultDocument',
'IIS-DirectoryBrowsing'
]
include_recipe 'iis::default'
include_recipe 'iis::mod_aspnet'
include_recipe 'iis::mod_auth_basic'
include_recipe 'iis::mod_auth_windows'
include_recipe 'iis::mod_compress_static'
include_recipe 'iis::mod_security'
include_recipe 'iis::mod_management'
if windows_version.windows_server_2012_r2?
include_recipe 'iis::mod_aspnet45'
include_recipe 'iis::mod_tracing'
include_recipe 'iis::mod_application_initialization'
end
include_recipe
如果方法正在运行(块中的所有内容) ,我希望能够使用 chefspec 进行测试if
。
看完后:
标准 RSpec 如此
allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('fake version'))
或类似地适用。来源: Chef unit/rspec 测试中的 Mock Chef::ReservedNames::Win32::Version.new?
我试图修改我install_iis_spec
的来模拟Chef::ReservedNames::Win32::Version
. 我的规范文件现在如下所示:
install_iis_spec:
recipes_2012 = [
'iis::mod_aspnet45',
'iis::mod_tracing',
'iis::mod_application_initialization'
]
context 'when on "Windows Server 2012 R2"' do
before do
allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('6.3'))
end
let(:chef_run) do
runner = ChefSpec::SoloRunner.new(platform: 'windows', version: '2012R2')
runner.converge(described_recipe)
end
should_include_recipe(recipes_2012)
it 'converges successfully' do
expect { chef_run }.to_not raise_error
end
end
注1:假设该should_include_recipe
方法按预期工作。
注2:看到后double('fake version')
,我认为我应该把价值'6.3'
。
虽然,当我运行时,chef exec rspec spec/unit/recipes/install_iis_spec.rb
我收到以下错误:
控制台错误:
my_recipe::install_iis when on "Windows Server 2012 R2" runs the 'iis::mod_aspnet45' recipe
Failure/Error: runner.converge(described_recipe)
#<Double "6.3"> received unexpected message :windows_server_2008? with (no args)
# C:/Users/me/AppData/Local/Temp/d20161018-26632-iyzn4f/cookbooks/iis/libraries/helper.rb:44:in `older_than_windows2008r2?'
# C:\Users\me\AppData\Local\Temp\d20161018-26632-iyzn4f\cookbooks\iis\recipes\default.rb:22:in `from_file'
# C:\Users\me\AppData\Local\Temp\d20161018-26632-iyzn4f\cookbooks\my_recipe\recipes\install_iis.rb:23:in `from_file'
# ./spec/unit/recipes/install_iis_spec.rb:61:in `block (3 levels) in <top (required)>'
# ./spec/spec_helper.rb:7:in `block (2 levels) in should_include_recipe'
参考: cookbooks/iis/libraries/helper.rb:44:in 'older_than_windows2008r2?' .
double('fake version')
为了定位,我必须输入什么值Windows Server 2012R2
?
有支持的版本列表吗?