Rspec 规范测试中的stubing chef_环境变量
在我的规范测试中对chef_env变量的刺伤有问题。配方的一部分: recipe.rbRspec 规范测试中的stubing chef_环境变量,rspec,chef-infra,chefspec,Rspec,Chef Infra,Chefspec,在我的规范测试中对chef_env变量的刺伤有问题。配方的一部分: recipe.rb example_credentials = data_bag_item(:credentials, 'example') template '/etc/nginx/sites-available/example.conf' do source 'http/example.conf.erb' owner 'root' group 'root' mode
example_credentials = data_bag_item(:credentials, 'example')
template '/etc/nginx/sites-available/example.conf' do
source 'http/example.conf.erb'
owner 'root'
group 'root'
mode '0644'
variables({
authorization: Base64.strict_encode64(example_credentials[node.chef_environment]['example_auth']),
cluster_id: node['project']['http']['example']['cluster_id']
})
end
require 'spec_helper'
describe 'project::http' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '16.04') do |node|
env = Chef::Environment.new
env.name 'test'
expect(node).to receive(:chef_environment).and_return env.name
expect(Chef::Environment).to receive(:load).and_return env
end.converge(described_recipe)
end
before(:each) do
stub_data_bag_item(:credentials, 'example').and_return(example_auth: 'test_value')
end
end
测试它:
测试.rb
example_credentials = data_bag_item(:credentials, 'example')
template '/etc/nginx/sites-available/example.conf' do
source 'http/example.conf.erb'
owner 'root'
group 'root'
mode '0644'
variables({
authorization: Base64.strict_encode64(example_credentials[node.chef_environment]['example_auth']),
cluster_id: node['project']['http']['example']['cluster_id']
})
end
require 'spec_helper'
describe 'project::http' do
let(:chef_run) do
ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '16.04') do |node|
env = Chef::Environment.new
env.name 'test'
expect(node).to receive(:chef_environment).and_return env.name
expect(Chef::Environment).to receive(:load).and_return env
end.converge(described_recipe)
end
before(:each) do
stub_data_bag_item(:credentials, 'example').and_return(example_auth: 'test_value')
end
end
但是我还是发现了一个错误
expected no Exception, got #<NoMethodError: undefined method `[]' for nil:NilClass> with backtrace
预期无异常,get#当您尝试访问哈希嵌套属性,但没有要查找的父属性时,通常会引发此错误
在您的情况下,存根凭证数据包并返回哈希:
{
示例_auth:“测试值”
}
那么在这一行:
授权:Base64.strict\u encode64(示例\u凭证[node.chef\u环境]['example\u auth']),
您正试图访问散列的“test”键(=node.chef_环境),但它不在那里。您需要更改存根数据包,这样它也有“测试”键
stub\u data\u bag\u项目(:凭证,'example')。并返回(
{
“测试”=>{
“示例验证”=>“测试值”
}
}
)
您的属性中是否设置了默认['test']['example\u auth']
设置?@DracoAter我添加了属性,但它没有做任何事情