无法在厨师食谱中运行rspec单元测试“ touch_file”

时间:2018-09-17 13:12:24

标签: unit-testing rspec chef chefspec

食谱: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?

2 个答案:

答案 0 :(得分:0)

应为expect(chef_run).to touch_file('C:\inetpub\wwwroot\iisstart.htm')。您只能将expect { ... }raise_error一起使用,类似的事情,大多数匹配项都将expect(...)用作普通呼叫。另外,touch_file之后还有多余的空格。 method (args)在Ruby中是不允许的(或者至少是允许的,并且不执行您认为的操作)。

答案 1 :(得分:0)

解决方案:

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