【发布时间】: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