我正在尝试使用 ChefSpec 测试角色.我宁愿不使用 Chef Zero (通过要求 'chefspec/server')只是因为它的运行速度比 ChefSpec 本身慢一点。
也许我读错了文档,但看起来似乎不需要 Chef Zero 来测试角色。然而,我目前的配置没有任何运气。这是我的测试:
require 'chefspec'
RSpec.configure do |config|
config.cookbook_path = 'C:\projects\chef\cookbooks'
config.role_path = 'C:\projects\chef\roles'
config.log_level = :debug
config.platform = 'ubuntu'
config.version = '12.04'
end
describe 'my_cookbook::default' do
let(:chef_run) do
ChefSpec::Runner.new.converge('role[my_role]')
end
it 'runs without failing' do
expect(chef_run)
end
end
角色(位于roles/my_role.json):
{
"name": "my_role",
"description": "My role",
"default_attributes": {
},
"run_list": [
"recipe[my_cookbook::default]"
]
}
当我运行测试时,我收到:
NoMethodError: undefined method `run_list_for' for #<Hash:0x4fa3280>
./spec/role_spec.rb:13:in `block (2 levels) in <top (required)>'
./spec/role_spec.rb:17:in `block (2 levels) in <top (required)>'
如果我修改我的测试以通过要求
chefspec/server
手动将角色加载到 Chef Zero ,这似乎有效。我不认为我应该从文档的编写方式来模拟服务器,但我可能是错的。难道我做错了什么?这是一个错误吗?还是我必须使用厨师零?
最佳答案
如果您使用 JSON 角色,则必须包含 json_class
:
"json_class": "Chef::Role"
如果您使用的是 Ruby 角色,则不需要这样做。
关于chef-infra - 无方法错误 : undefined method `run_list_for' when testing role with ChefSpec,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21442236/