Cas van Noort
Cas van Noort

Reputation: 255

ChefSpec doesn't find my Chef resources

I have a cookbook with a bunch of recipes and LWRP's. I now want to start using ChefSpec for coverage, and wrote one example:

require 'chefspec'
require 'chefspec/berkshelf'

ChefSpec::Coverage.start!

RSpec.configure do |config|
  config.platform = 'debian'
  config.version = '7.0'
end

describe 'zilvercms-debian::default' do
  let(:chef_run) { ChefSpec::Runner.new(platform: 'debian', version: '7.0').converge(described_recipe) }

  it 'includes the `timezone-ii::debian` recipe' do
    expect(chef_run).to include_recipe('timezone-ii::debian')
  end
end

Result of my run:

~/cookbooks/test-debian$ rspec
[2014-07-21T15:57:58+02:00] WARN: Cloning resource attributes for execute[apt-get-update] from prior resource (CHEF-3694)
[2014-07-21T15:57:58+02:00] WARN: Previous execute[apt-get-update]: /tmp/d20140721-9408-jceoey/cookbooks/apt/recipes/default.rb:29:in `from_file'
[2014-07-21T15:57:58+02:00] WARN: Current  execute[apt-get-update]: /tmp/d20140721-9408-jceoey/cookbooks/apt/recipes/default.rb:38:in `from_file'
.

Finished in 0.16909 seconds (files took 2.29 seconds to load)
1 example, 0 failures

  No Chef resources found, skipping coverage calculation...

Does anyone maybe have any clue why ChefSpec doesn't notice my resources?


EDIT:

Moved ChefSpec::Coverage.start! as suggested by Seth.

Here is a simple recipe where it happens:

# Set timezone for debian machine
include_recipe 'timezone-ii::debian'

# Include recipe for managing repositories on Debian and automatic updating
include_recipe 'debian::default'

# Set NTP for Debian
include_recipe 'ntp::default'

Upvotes: 1

Views: 2101

Answers (1)

sethvargo
sethvargo

Reputation: 26997

According to the ChefSpec Reporting documentation, you must include the coverage call before anything else:

To generate the coverage report, add the following to your spec_helper.rb before you require any "Chef" code:

require 'chefspec'
ChefSpec::Coverage.start!

In your code, you are calling the coverage report after the specs have already run. Move the:

ChefSpec::Coverage.start!

before your describe block.

Upvotes: 1

Related Questions