chef-infra - 无方法错误 : undefined method `run_list_for' when testing role with ChefSpec

标签 chef-infra chefspec

我正在尝试使用 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/

相关文章:

php - 有什么方法可以用 Chef 运行 'cake schema create' 吗?

ruby - Chef 属性文件

rspec - 在chefspec中打开 stub 文件

unit-testing - 如何创建在另一个配方中定义的虚拟资源而不在测试运行中包含另一个配方?

ruby - ChefSpec 不应测试包含的 Recipe

chef-infra - 在重构环境/角色/节点时如何使用 ChefSpec

chef-infra - Vagrant 问题

logging - Chef::Log.info 到底做了什么

chef-infra - 在 Windows Server 上安装 Chef Solo

ruby - ChefSpec 和 Ark 资源测试