0
$ cat Gemfile | grep "'chefspec'\|'chef'"
  gem 'chef', '12.8.1'
  gem 'chefspec', '4.6.1'

$ cat cookbooks/foo/recipes/default.rb | grep include
include_recipe 'bar'

$ cat cookbooks/foo/metafata.rb | grep depends
depends 'bar'

running chefspec output Untouched Resources: for all resources in bar cookbook. i followed include recipe assertion, but it uses allow_any_instance_of, which might not be a best practice. i've used it as follows:

$ cat cookbook/foo/spec/recipes/default_spec.rb
describe 'foo::default' do
  cached(:chef_run) { ChefSpec::ServerRunner.converge(described_recipe) }
  let(:recipe) { instance_double(Chef::Recipe, cookbook_name: 'foo', recipe_name: 'default') }

  before do
    allow_any_instance_of(Chef::Recipe).to receive(:include_recipe).and_call_original
    allow_any_instance_of(recipe).to receive(:include_recipe).with('bar')
  end

  it 'includes recipes' do
    expect_any_instance_of(recipe).to receive(:include_recipe).with('bar')
  end
end

but when i run rspec i get the follwing error:

foo::default
  includes recipes (FAILED - 1)

Failures:

  1) foo::default includes recipes
     Failure/Error: allow_any_instance_of(recipe).to receive(:include_recipe).with('bar')
       #<InstanceDouble(Chef::Recipe) (anonymous)> received unexpected message :ancestors with (no args)
     # ./vendor/cookbooks/foo/spec/recipes/default_spec.rb:32:in `block (2 levels) in <top (required)>'

Finished in 2.22 seconds (files took 1.72 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./vendor/cookbooks/foo/spec/recipes/default_spec.rb:43 # foo::default includes recipes

can one shed the light on the issue:

  1. can chefspec ran the spec of all included cookbooks? if so, how i can be configured?
  2. if the previous is not possible, how can i fix the above and eliminate the converge of included cookbooks (treat other cookbooks as a blackbox)?

UPDATE:

tried also the solution Stub (...) received unexpected message (...) with (no args) which did not help, and returned a different error:

foo::default
  includes recipes (FAILED - 1)

Failures:

  1) foo::default includes recipes
     Failure/Error: allow(recipe).to receive(:ancestors).and_return(true)
       the Chef::Recipe class does not implement the instance method: ancestors. Perhaps you meant to use `class_double` instead?
     # ./vendor/cookbooks/foo/spec/recipes/default_spec.rb:31:in `block (2 levels) in <top (required)>'
     # ./vendor/cookbooks/foo/spec/recipes/default_spec.rb:37:in `block (2 levels) in <top (required)>'

Finished in 1.33 seconds (files took 1.4 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./vendor/cookbooks/foo/spec/recipes/default_spec.rb:48 # foo::default includes recipes
Community
  • 1
  • 1
Mr.
  • 9,429
  • 13
  • 58
  • 82
  • 1
    If I understand you properly, what you wish really is to exclude 'bar' cookbook resources from coverage report, and for this it's documented in the coverage part of the Readme [here](https://github.com/sethvargo/chefspec#reporting) using `add_filter` in the `coverage.start!` block. Chefspec does not load the tests for included cookbook, I don't know of a way to do this, including the helpers/stubs could be an idea, but it's too much guessing IMO. – Tensibai Apr 18 '16 at 08:44
  • @Tensibai: first **thank you** for your reply. note that, adding a filter does not mean the cookbook will be out of the run list, it will just won't be in the coverage report, which implies that stubs\mocks created for the included cookbook (`bar` in our case) will have to be duplicated into the cookbook the includes other cookbook (`foo` in our case), and we code duplication is bad. more of which, if there is a single `spec_helper` for all my private cookbook, the filter which is set globally or in a central place, needs to be modified per cookbook. do you have any other idea? – Mr. Apr 18 '16 at 09:16
  • ChefSpec is "cookbook centric", you should not have a common spec_helper for many cookbooks. Assuming you're knowing what you're doing, you can require the other cookbook specs in turn. Preventing the wrapped cookbook resources from running is usually a wrong pattern as you may miss overwriting of ressources too. So tl;dr; You'll have to have code duplication, which is not that bad as it will tell you if an update of the wrapped cookbook breaks yours ;) – Tensibai Apr 18 '16 at 10:18
  • @Tensibai: i've utilized what you offered in your previous comment - in the centric `spec_helper`, i extended my custom filter [(`ChefSpec::Coverage::Filter`)](https://github.com/sethvargo/chefspec#reporting), to have a class (static) variable, which holds an exclusion list, and the variable is accessible externally (from within a specific spec). i guess you got me here... i would like to give you a credit about the lead, so please convert your comment to an answer so i could mark it a resolved. – Mr. Apr 18 '16 at 12:31
  • You should write your own answer with the details solving your problem, I just gave tips here ;) – Tensibai Apr 18 '16 at 12:34
  • @Tensibai: my implementation concerns only me, though i used your suggestion :) – Mr. Apr 18 '16 at 13:54

1 Answers1

0

taken from Tensibai comment:

If I understand you properly, what you wish really is to exclude 'bar' cookbook resources from coverage report, and for this it's documented in the coverage part of the Readme here using add_filter in the coverage.start! block. Chefspec does not load the tests for included cookbook, I don't know of a way to do this, including the helpers/stubs could be an idea, but it's too much guessing IMO.

Mr.
  • 9,429
  • 13
  • 58
  • 82