Mock Chef::ReservedNames::Win32::Version.new в тесте юнитов Chef /rspec? [Продолжение]

Предполагая, у меня есть следующий рецепт:

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 в тесте Chef unit/rspec?

Я пытался изменить мой install_iis_spec издеваться Chef::ReservedNames::Win32::Version, Мой spec-файл теперь выглядит следующим образом:

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'

Ссылка: кулинарные книги /iis/library /helper.rb:44: в 'old_than_windows2008r2?',


Какую ценность я должен поставить, в double('fake version') для того, чтобы нацелиться Windows Server 2012R2?

Есть ли список поддерживаемых версий?

1 ответ

Решение

'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, в документации RSpec или в любом из нескольких тысяч учебных пособий, которые, я уверен, доступны через Google.

Другие вопросы по тегам