簡體   English   中英

RSpec 3未定義方法`允許&#39;為# <RSpec::Core::ExampleGroup…>

[英]RSpec 3 undefined method `allow' for #<RSpec::Core::ExampleGroup…>

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

我正在嘗試為這個主題分配一個屬性......我似乎無法使咒語正確。 我需要雙人嗎? 我不確定它是如何工作的,我顯然無法破譯文檔。 任何幫助表示贊賞。

我認為這里的問題是范圍之一。 在設置subject之前,您正在調用before塊中的代碼

你需要將subject移動到外部上下文中,將before移動到內部描述中,或者設置一些替代方法來調用它,以便在運行before設置subject

我認為你應該使用letsubject變量定義為輔助方法。 有了這個,您將定義一個可以在文件中的任何位置使用的輔助方法。

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

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 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM