【问题标题】:Mock Chef::ReservedNames::Win32::Version.new in Chef unit/rspec test? [continued]在 Chef 单元/rspec 测试中模拟 Chef::ReservedNames::Win32::Version.new? [继续]
【发布时间】:2016-10-31 03:46:38
【问题描述】:

假设,我有以下食谱:

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 方法是否正在运行(if 块中的所有内容)。


阅读后:

标准 RSpec 适用于allow(Chef::ReservedNames::Win32::Version).to receive(:new).and_return(double('fake version')) 或类似。

来源: Mock Chef::ReservedNames::Win32::Version.new in Chef unit/rspec test?

我试图修改我的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?'.


为了定位Windows Server 2012R2,我必须在double('fake version') 中输入什么值?

有支持的版本列表吗?

【问题讨论】:

    标签: rspec chef-infra chefspec


    【解决方案1】:

    '6.3' 只是假版本对象的标签。你需要告诉它如何响应方法。在这方面,这很简单:double('fake version', :windows_server_2008? =&gt; 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 获得的数千个教程中的任何一个中找到有关如何使用 RSpec 的更多信息。

    【讨论】:

    • 感谢您为我解决问题。添加:windows_server_2008? =&gt; false 可以解决该部分的问题。一旦我这样做了,我就会收到windows_vista? 的错误,这是列表中的下一个。我相信我必须将所有这些添加到and_return,尽管我只是想知道是否有更好的方法来做到这一点?谢谢。
    • 这就是 mocking 的工作方式,您需要专门编写每个方法。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-04
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多