代码之家  ›  专栏  ›  技术社区  ›  user3066571

尝试安装多窗口功能时Chefspec不工作

  •  0
  • user3066571  · 技术社区  · 8 年前

    我有一个为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块这一事实,我不太清楚为什么会出错。有人有什么想法吗?

    1 回复  |  直到 4 年前
        1
  •  1
  •   jedifans    8 年前

    在您的规范中,您的厨师运行在let块中聚合。移动您的 stub_command() 呼叫位于 before do ... end 阻止帮助?

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