Question

The default recipe for my cookbook simply includes several other recipes. I know that I can test that the appropriate recipes are included using:

expect(chef_run).to include_recipe 'cookbook::recipe_name

but this doesn't work when I've stubbed the include_recipe call like so

Chef::Recipe.any_instance.stub(:include_recipe).with(anything).and_return(true)

I'm stubbing the include_recipe call as I don't want to have to define all the missing attributes for all of my recipes in this spec.

I would have thought that by stubbing the method to return true ChefSpec would report it as having run and consider the recipes included but it doesn't. Any ideas?

Was it helpful?

Solution

I was going to post above as a question but I found a solution posted by 'RoboticCheese' on github so I'm answering my own question as it might help others.

You do stub include recipe as above but you also need to stub some RunContext methods to count the recipes as having been included.

OTHER TIPS

I think I have an updated and cleaner solution to this if you want to ensure include_recipe is called with the right recipe(s), but don't want to load the resources in the included recipe(s):

before(:all) { @included_recipes = [] }
before do
  @stubbed_recipes = %w[test_cookbook::included_recipe apt]
  @stubbed_recipes.each do |r|
    allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).with(r) do
      @included_recipes << r
    end
  end
end

it 'includes the correct recipes' do
  chef_run
  expect(@included_recipes).to match_array(@stubbed_recipes)
end

A complete example of this: https://github.com/atheiman/test-cookbook/pull/4

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top