2014-10-29 112 views
4
describe '#messages' do 
    subject do 
    FactoryGirl.create :foo, 
     :type => 'test', 
     :country => 'US' 
    end 

    context 'when is not U.S.' do 
    before{ allow(subject).to receive(:country).and_return('MX') } 

    describe '#messages' do 
     subject { super().messages } 
     it { is_expected.to include 'This foo was not issued in the United States of America.' } 
    end 
    end 
end 

我想分配一個屬性的主題......我似乎無法得到咒語正確。我需要一個Double嗎?我不確定這是如何工作的,而且我顯然無法破譯文檔。任何幫助表示讚賞。RSpec 3未定義的方法`允許'爲#<RSpec :: Core :: ExampleGroup ...>

回答

0

我認爲這裏的問題是一個範圍。您呼叫的before塊中的代碼,你已經設置了前subject

你要麼需要進行移動subject到外背景下,移動before入內描述,或者設置一些替代方法調用它,以便在運行之前設置subjectbefore

0

我認爲您應該將subject變量定義爲使用let的輔助方法。有了這個,你正在定義一個助手方法,你可以在文件中的任何地方使用它。

所以,如下我將修改代碼:

describe '#messages' do 
    let(:subject) do 
    FactoryGirl.create :foo, 
     :type => 'test', 
     :country => 'US' 
    end 

    context 'when is not U.S.' do 
    before{ allow(subject).to receive(:country).and_return('MX') } 

    describe '#messages' do 
     subject { super().messages } 
     it { is_expected.to include 'This foo was not issued in the United States of America.' } 
    end 
    end 
end 
相關問題