模拟 Chef::ReservedNames::Win32::Version.Chef unit/rspec 测试中的新功能? [继续]

Mock Chef::ReservedNames::Win32::Version.new in Chef unit/rspec test? [continued]

假设,我有以下食谱:

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

我希望能够使用 chefspec 测试 include_recipe 方法是否 运行ning(if 块中的所有内容)。


看完后:

Standard RSpec applies so allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('fake version')) or similar.

Source:

我试图修改我的 install_iis_spec 来模拟 Chef::ReservedNames::Win32::Version。我的规范文件现在如下所示:

install_iis_规格:

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

是否有支持的版本列表?

'6.3'只是假版本对象的标签。您需要告诉它如何响应方法。在这方面,这很简单:double('fake version', :windows_server_2008? => false)(或者 true,如果你想假装是真的)。通常您会在 before 块中执行此操作,如下所示:

before do
  allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('fake version', :windows_server_2008? => false))
end

您可以在 RSpec 文档或我确信可以通过 Google.[=15 获得的数千个教程中的任何一个中找到有关如何使用 RSpec 的更多信息=]