所以我正在尝试为我的一个厨师食谱编写一个单元测试(通过 ChefSpec),但我得到了一些奇怪的行为。
我有问题的配方包括 rvm::system_install 配方,显然这会导致 ChefSpec 出现问题:
Failures:
1) theoracle::prereqs installs libyaml libyaml-devel sqlite-devel
Failure/Error: let(:chef_run) { ChefSpec::Runner.new.converge('theoracle::prereqs')}
NoMethodError:
undefined method `each' for nil:NilClass
# /Users/jdoe/workspace/cookbooks/rvm/libraries/chef_rvm_recipe_helpers.rb:44:in `install_pkg_prereqs'
# /Users/jdoe/workspace/cookbooks/rvm/recipes/system_install.rb:29:in `from_file'
# ./recipes/prereqs.rb:22:in `from_file'
# ./spec/unit/recipes/prereqs_spec.rb:6:in `block (2 levels) in <top (required)>'
# ./spec/unit/recipes/prereqs_spec.rb:15:in `block (2 levels) in <top (required)>'
有问题的代码段:
/Users/jdoe/workspace/cookbooks/rvm/libraries/chef_rvm_recipe_helpers.rb:
41: def install_pkg_prereqs(install_now = node.recipe?("rvm::gem_package"))
42: return if mac_with_no_homebrew
43:
44>> node[:rvm][:install_pkgs].each do |pkg|
45: p = package pkg do
这就是我的测试的样子:
require 'spec_helper'
describe 'theoracle::prereqs' do
let(:chef_run) { ChefSpec::Runner.new.converge('theoracle::prereqs')}
it 'installs libyaml libyaml-devel sqlite-devel' do
%W( libyaml libyaml-devel sqlite-devel ).each do |pkg|
expect(chef_run).to_install_package(pkg)
end
end
end
这是theoracle::prereqs
食谱(部分):
%W( libyaml libyaml-devel sqlite-devel ).each do |pkg|
package pkg do
action :install
end
end
include_recipe 'rvm::system_install'