我有一个成功的资源:(融合,按预期工作)
cron_d 'zk_metric' do
minute '*'
command “something something"
end
但添加规格后
it 'add cron_d' do
expect(chef_run).to create_cron_d('zk_metric')
end
chefspec
收到错误:
Failures:
1) myorg::myrecipe add cron_d
Failure/Error: expect(chef_run).to create_cron_d('zk_metric')
NoMethodError:
undefined method `create_cron_d' for #<RSpec::ExampleGroups::Myorgmyrecipe:0x007fa726086e50>
# ./spec/myrecipe_spec.rb:93:in `block (2 levels) in <top (required)>'
Finished in 12.17 seconds (files took 1.08 seconds to load)
为什么会这样?
匹配器已经定义为https://github.com/opscode-cookbooks/cron/blob/master/libraries/matchers.rb
我的spec文件中是否需要include
或require
(到目前为止还没有工作)?或者我需要创建自己的?
(编辑:stackoverflow autobot让我添加ruby-on-rails标签,所以我做了。)
答案 0 :(得分:0)
在spec_helper.rb
或规范文件的开头添加以下行。
require 'chefspec'
答案 1 :(得分:0)
你错过了一些东西,而且你有一个间距错字。
以下是如何使用示例:
script.py
)。cron
食谱<强> cron_recipe.rb 强>
include_recipe "cron"
cookbook_file '/etc/cron.d/script.py' do
source 'folder/script.py'
owner 'root'
group 'root'
mode '0777'
action :create
end
cron_d "example_cron" do
minute '0'
hour '0'
command '/etc/cron.d/script.py'
user 'root'
end
现在您可以创建规范:
<强> cron_recipe_spec.rb 强>
# encoding: UTF-8
require 'spec_helper'
describe 'cookbook::cron_recipe' do
CookbookTest.contexts.each do |ctext|
context '#{ctext[:platform]}-#{ctext[:version]}' do
cached(:chef_run) do
CookbookTest.runner(ctext).converge(described_recipe)
end
it 'includes the cron_recipe that we are testing' do
expect(chef_run).to include_recipe(described_recipe)
end
it 'includes the cron recipe' do
expect(chef_run).to include_recipe('cron')
end
it 'creates a script.py file' do
expect(chef_run).to create_cookbook_file('/etc/cron.d/script.py').with(
:user => 'root',
:group => 'root',
:mode => '0777'
)
end
it 'creates the cron' do
expect(chef_run).to create_cron_d('example_cron').with(
:command => '/etc/cron.d/script.py',
:user => 'root',
:minute => '0',
:hour => '0'
)
end
end
end
end