#<RSpec::Core::ExampleGroup...> 的 RSpec 3 未定义方法“允许”

Dud*_*udo 5 ruby rspec ruby-on-rails

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
Run Code Online (Sandbox Code Playgroud)

我正在尝试为该主题分配一个属性......我似乎无法正确地获得咒语。我需要双人吗?我不确定它是如何工作的,而且我显然无法破译文档。任何帮助表示赞赏。

Tar*_*ast 0

我认为这里的问题是范围之一。before您在设置之前调用块中的代码subject

您需要将其移至subject外部上下文,将其移至before内部描述,或者设置某种调用它的替代方式,以便subject在运行之前进行设置before