2015-08-20 27 views
1

基本上,我有一堆共享的例子。我認爲這會工作,但我得到正在傳遞的變量stack level too deep如何在嵌套的Rspec共享示例中傳遞變量兩個級別?

shared_examples 'Foo' do 
    # top level shared example passes values 
    # to a lower-level shared example. 
    it_behaves_like 'Bar' do 
    let(:variable) { variable } 
    end 

    it_behaves_like 'Baz' 
end 

shared_examples 'Bar' do 
    it { expect(variable).to veq('value') 
end 

規範:

describe SomeClass do 
    it_behaves_like 'Foo' do 
    let(:variable) { 'value' } 
    end 
end 

我想分享的例子保持自己的背景下,這是爲什麼造成的問題嗎?

+1

我想你在代碼中有一個遞歸:'let(:variable){variable}'調用它自己 – mhutter

回答

1

你在你的代碼有遞歸:

let(:variable) { variable } 

會打電話給自己一遍又一遍


功能會自動向下傳遞的變量,所以這將工作:

shared_examples 'Foo' do 
    it_behaves_like 'Bar' 
    it_behaves_like 'Baz' 
end 

shared_examples 'Bar' do 
    it { expect(variable).to eq('value') } 
end 

and

describe SomeClass do 
    let(:variable) { 'value' } 
    it_behaves_like 'Foo' 
end