2016-09-12 59 views
0

我試圖用ChefSpec測試的廚師和Hashicorp庫Chefspec與Hashicorp庫

配方

chef_gem 'vault' do 
    compile_time true if Chef::Resource::ChefGem.instance_methods(false).include?(:compile_time) 
end 

require 'vault' 

Vault.address = 'https://address:8200' 
Vault.token = citadel['foo/bar'] 
Vault.auth_token.renew_self 

測試

require_relative '../spec_helper' 

describe 'wrapper::default' do 
    context 'role is foo' do 
    let(:chef_run) do 
     ChefSpec::SoloRunner.new(platform: 'ubuntu', version: '14.04') do |node| 
     node.default['role'] = 'foo' 
     const_set(:Vault, Module.new) 
     end.converge(described_recipe) 
    end 

    before(:each) do 
     allow_any_instance_of(Chef::Recipe).to receive(:require).and_call_original 
     allow_any_instance_of(Chef::Recipe).to receive(:require).with('vault').and_return(true) 
     allow_any_instance_of(::Vault).to receive(:address).and_call_original 
     allow_any_instance_of(::Vault).to receive(:address).with('https://localhost:8200').and_return(true) 
    end 

    it 'install vault gem' do 
     expect(chef_run).to install_chef_gem('vault') 
    end 
    end 
end 

錯誤

Failure/Error: expect(Chef::Recipe::Vault).to receive(:address).and_call_original 

    NameError: 
     uninitialized constant Vault 
實現

如何將Vault va存根riables?這是Hashicorp Vault,而不是廚師保險庫。

回答

1

我已回覆您的電子郵件,您需要allow_any_instance_of(::Vault)以及類似的內容,如果它不存在,您可能必須創建該模塊(const_set(:Vault, Module.new))。

+0

更新我的問題,我添加了一個常量,並拿出Chef :: Recipe,仍然沒有初始化常量 – jsmickey

+0

請參閱上面的第二個位,您需要在您之前使用'const_set'或'stub_const'創建模塊可以在它上面留下一些東西。 – coderanger