Chefspec没有针对数组进行迭代

时间:2016-05-23 19:39:26

标签: arrays rspec stub chefspec

我有一个安装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

当我在cookbook上运行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显示为&#39; WebServerRole&#39;,它为每一行(30个条目)执行此操作。指示它仅在该阶段迭代第一个项目。结合我肯定使用stub_command块的事实,我不确定它为什么会出错。有人有什么想法吗?

1 个答案:

答案 0 :(得分:1)

在您的规范中,您的厨师运行正在汇集在let块中。将您的stub_command()来电转移到before do ... end区块有帮助吗?

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