【问题标题】:Unable to run rspec unit test "touch_file" in chef cookbook无法在厨师食谱中运行 rspec 单元测试“touch_file”
【发布时间】:2018-09-20 21:03:16
【问题描述】:

食谱:https://github.com/tkidd77/devops-project/tree/master/chef-repo/cookbooks/hello_world

我的单元测试:

it 'touches the correct file' do
  expect { chef_run }.to touch_file ('C:\inetpub\wwwroot\iisstart.htm')
end

在 git bash 中运行“chef exec rspec”时的输出:

tkidd@tkiddhome MINGW64 /c/git/project/chef-repo/cookbooks/hello_world (master) $ chef exec rspec .F

Failures:

  1) hello_world::default touches the correct file
     Failure/Error: expect { chef_run }.to touch_file ('C:\inetpub\wwwroot\iisstart.htm')
       You must pass an argument rather than a block to use the provided matcher ( file "C:\inetpub\wwwroot\iisstart.htm"), or the matcher must implement `supports_block_expectations?`.
     # ./spec/unit/recipes/default_spec.rb:38:in `block (2 levels) in <top (required)>'

Finished in 0.07199 seconds (files took 8.68 seconds to load) 2 examples, 1 failure

Failed examples:

rspec ./spec/unit/recipes/default_spec.rb:37 # hello_world::default touches the correct file

这里是关于使用 touch_file 测试的 chefspec 文档:https://www.rubydoc.info/github/acrmp/chefspec/ChefSpec/API/FileMatchers 它指定使用括号而不是“chef-run”周围的括号,但是当我这样做时,我收到一个“未定义的方法”错误:

tkidd@tkiddhome MINGW64 /c/git/project/chef-repo/cookbooks/hello_world (master) $ chef exec rspec .F

Failures:

  1) hello_world::default touches the correct file
     Failure/Error: expect (chef_run).to touch_file ('C:\inetpub\wwwroot\iisstart.htm')

     NoMethodError:
       undefined method `to' for #<ChefSpec::SoloRunner:0x0000000007a241d8>
     # ./spec/unit/recipes/default_spec.rb:38:in `block (2 levels) in <top (required)>'

Finished in 0.04001 seconds (files took 4.92 seconds to load) 2 examples, 1 failure

Failed examples:

rspec ./spec/unit/recipes/default_spec.rb:37 # hello_world::default touches the correct file

据此,rspec 3.0 需要一个方法而不是文件路径的块,但我不明白那会是什么样子。 How to check whether a variable is an instance of a module's subclass using rspec?

【问题讨论】:

    标签: unit-testing rspec chef-infra chefspec


    【解决方案1】:

    应该是expect(chef_run).to touch_file('C:\inetpub\wwwroot\iisstart.htm')。你只使用expect { ... }raise_error 之类的东西,大多数匹配使用expect(...) 作为正常调用。在touch_file 之后还有一个额外的空间。 method (args) 在 Ruby 中是不允许的(或者至少它是允许的,但不会像你认为的那样做)。

    【讨论】:

    • 该语法导致:失败:1) hello_world::default 触及正确的文件失败/错误:expect(chef_run).to touch_file('C:\inetpub\wwwroot\iisstart.htm') NoMethodError: ' 中未定义的方法resource_collection' for nil:NilClass # ./spec/unit/recipes/default_spec.rb:38:in block(2 级)
    • 那么您的规范代码的其余部分不正确设置chef_run 对象。
    • 这是合乎逻辑的,但这里似乎并非如此,因为其他单元测试使用相同的 chef_run 对象:describe 'hello_world::default' do let :chef_run do ChefSpec::SoloRunner .new(platform: 'windows', version: '2016') end it 'converges successful' do expect { chef_run }.to_not raise_error end
    • 你缺少一个 .converge(something)
    【解决方案2】:

    解决方案:

    describe 'hello_world::default' do
    
      let :chef_run do
        ChefSpec::SoloRunner.new(platform: 'windows', version: '2016').converge(described_recipe)
      end
    
         it 'touches the correct file' do
           expect(chef_run).to touch_file('C:\inetpub\wwwroot\iisstart.htm')
         end
    end
    

    【讨论】:

      猜你喜欢
      • 2020-09-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-27
      相关资源
      最近更新 更多