我对Nginx食谱有一个简单的测试:
require 'spec_helper'
describe 'my_cookbook::nginx' do
let(:chef_run) do
ChefSpec::Runner.new do |node|
node.set['nginx']['dir'] = '/etc/nginx'
end.converge(described_recipe)
end
it 'should create configuration directory' do
expect(chef_run).to create_directory("#{node['nginx']['dir']}")
end
end
哪一个失败了:
Failures:
1) my_cookbook::nginx should create configuration directory
Failure/Error: expect(chef_run).to create_directory("#{node['nginx']['dir']}")
NameError:
undefined local variable or method `node' for #<RSpec::Core::ExampleGroup::Nested_1:0x00000007993570>
我正在尝试按照in the docs所述设置节点属性,是否有明显的缺失?
最佳答案
您可以设置节点属性。如果您查看stacktrace,它会提示这一行:
expect(chef_run).to create_directory("#{node['nginx']['dir']}")
具体来说,是
#{node['nginx']['dir']}
。您应在此处使用静态值,否则测试将毫无意义。更改为:expect(chef_run).to create_directory('/etc/nginx')
关于chef-infra - ChefSpec-无法设置节点属性,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21762765/