2015-03-03 55 views
2

可以說我有以下測試:如何覆蓋rspec-puppet中的hiera_data?

context 'test' do 
    let(:hiera_data) { { :number => '2' } } 

    it { should have_module__define_resource_count(2) } 
end 

context 'test2' do 
    let(:hiera_data) { { :number => '10' } } 

    it { should have_module__define_resource_count(10) } 
end 

第一個測試通過,但在第二次測試運行失敗,因爲hiera變量number仍然是2

似乎let(:hiera_data)是無法覆蓋之前聲明的變量。

根據this readme它應該工作,如果hiera數據設置在不同的文件,但它不起作用。

如何在一個specfile中多次測試hiera?

+0

最近維護的一個叉子您可以粘貼完整的測試嗎? – 2015-03-04 11:39:46

+0

@FelixFrank該問題已更新 – 030 2015-03-04 12:44:15

回答

3

我遇到了同樣的問題,被困了一段時間。最終我發現hiera的信息是。您可以修復它,如下所示:

let(:facts) {{ :cache_bust => Time.now }} 

因此,例如:

木偶代碼:

class example::bar { 
    notify { 'bar': message => hiera('bar_message') } 
} 

rspec的木偶代碼:

describe "example::bar" do 

    let(:facts) {{ :cache_bust => Time.now }} 

    describe "first in-line hiera_data test with a" do 
    let(:hiera_data) { { :bar_message => "a" } } 
    it { should contain_notify("bar").with_message("a") } 
    end 

    describe "second in-line hiera_data test with b" do 
    let(:hiera_data) { { :bar_message => "b" } } 
    it { should contain_notify("bar").with_message("b") } 
    end 
end 

這工作! I have a PR to add it to the docs. botfish fork是hiera-puppet-helper(see the note on the original