0

我有一个为 IIS 安装组件的方法。windows_feature由于有很多,它是一个输入安装块的数组。像这样:

 strings.each do |st|
     windows_feature st do
        guard_interpreter :powershell_script
         not_if "$ret = Get-WindowsOptionalFeature -Online -FeatureName #{st}; if ($ret.State -eq 'Disabled' ) { return 'false'} else {return 'true'}"
         action :install
     end
 end

我关联的 Chefspec 块具有相同的数组内容馈入其中。块是这样的:

describe 'HEQIIS::IIS' do
     let(:chef_run) { ChefSpec::SoloRunner.converge('HEQIIS::IIS') }
   strings.each do |st|
     it "installs_#{st}" do
       stub_command("$ret = Get-WindowsOptionalFeature -Online -FeatureName #{st}; if ($ret.State -eq 'Disabled' ) { return 'false'} else {return 'true'}").and_return(false)
       expect(chef_run).to install_windows_feature("#{st}")
     end
   end
 end

当我在食谱上运行 Chefspec 时,出现错误:

HEQIIS::IIS installs_IIS-LegacyScripts
      Failure/Error: let(:chef_run) { ChefSpec::SoloRunner.converge('HEQIIS::IIS') }

      ChefSpec::Error::CommandNotStubbed:
        Executing a real command is disabled. Unregistered command:

            command("$ret = Get-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole; if ($ret.State -eq 'Disabled' ) { return 'false'} else {return 'true'}")

        You can stub this command with:

            stub_command("$ret = Get-WindowsOptionalFeature -Online -FeatureName IIS-WebServerRole; if ($ret.State -eq 'Disabled' ) { return 'false'} else {return 'true'}").and_return(...)
      # ./heqiis/spec/default_spec.rb:70:in `block (2 levels) in <top (required)>'
      # ./heqiis/spec/default_spec.rb:75:in `block (3 levels) in <top (required)>'

在错误中,它将 -Featurename 显示为“WebServerRole”,它为每一行(30 个条目)执行此操作。表明它只是在那个阶段迭代第一个项目。再加上我肯定在使用 stub_command 块这一事实,我不确定它为什么会出错。有人有什么想法吗?

4

1 回答 1

1

在您的规范中,您的 chef 运行正在融合到 let 块中。将您的stub_command()电话转移到一个before do ... end街区有帮助吗?

编辑:确实,您的配方文件正在设置整个数组的命令,这些命令需要为每个规范的 expect() 调用存根。在 before 块内添加第二个循环,用于删除所有命令将解决您的问题。

于 2016-08-12T19:12:03.807 回答