NoMethodError: undefined method `run_list_for' when testing role with ChefSpec

chef-infra, chefspec

Solution

If you are using JSON roles, you must include the `json_class`:

"json_class": "Chef::Role"

If you are using Ruby roles, you don't need to do that.

Problem

I'm trying to test a role with ChefSpec. I would rather not use Chef Zero (by requiring 'chefspec/server') just because it runs a bit slower than ChefSpec does on it's own. Perhaps I'm reading the documentation wrong, but it doesn't look like Chef Zero is required to test a role. I haven't had any luck with my current configuration, however. This is my test: ``` 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 ``` The role (located at roles/my_role.json): ``` { "name": "my_role", "description": "My role", "default_attributes": { }, "run_list": [ "recipe[my_cookbook::default]" ] } ``` When I run the test, I receive: ``` 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)>' ``` If I modify my test to manually load the role into Chef Zero by requiring `chefspec/server`, that seems to work. I don't think I should have to mock the server from the way the documentation is written, but I could be wrong. Am I doing something wrong? Is this a bug? Or do I have to use Chef Zero?

Original source