ChefSpec找不到我的Chef资源

时间:2014-07-21 14:04:22

标签: ruby unit-testing rspec chef chefspec

我有一本食谱和一堆食谱和LWRP。我现在想开始使用ChefSpec进行覆盖,并写了一个例子:

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

我跑步的结果:

~/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...

有没有人可能知道为什么ChefSpec没有注意到我的资源?


修改

按照Seth的建议移动了ChefSpec::Coverage.start!

这是一个简单的配方:

# 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'

1 个答案:

答案 0 :(得分:1)

根据the ChefSpec Reporting documentation,您必须在> 之前加入覆盖率

  

要生成覆盖率报告,请在需要任何“Chef”代码之前将以下内容添加到spec_helper.rb:

require 'chefspec'
ChefSpec::Coverage.start!

在您的代码中,您在规范已经运行后调用覆盖率报告。移动:

ChefSpec::Coverage.start!
describe阻止之前