Ruby on rails RSpec 3未定义的方法'allow';对于#<;RSpec::Core::ExampleGroup…>;
我正在尝试为主题指定一个属性。。。我似乎无法正确理解咒语。我需要双人间吗?我甚至不知道这是怎么回事,而且我显然无法解读这些文件。非常感谢您的帮助。我认为这里的问题是范围问题。在设置Ruby on rails RSpec 3未定义的方法'allow';对于#<;RSpec::Core::ExampleGroup…>;,ruby-on-rails,ruby,rspec,Ruby On Rails,Ruby,Rspec,我正在尝试为主题指定一个属性。。。我似乎无法正确理解咒语。我需要双人间吗?我甚至不知道这是怎么回事,而且我显然无法解读这些文件。非常感谢您的帮助。我认为这里的问题是范围问题。在设置主题之前,您正在调用before块中的代码 您需要将主题移到外部上下文中,将之前的移到内部描述中,或者设置另一种调用方法,以便在运行之前的之前设置subject我认为应该使用将subject变量定义为辅助方法。这样,您就定义了一个助手方法,可以在文件中的任何地方使用它 因此,我将修改您的代码,如下所示: describ
主题之前,您正在调用before
块中的代码
您需要将主题
移到外部上下文中,将之前的移到内部描述中,或者设置另一种调用方法,以便在运行之前的之前设置subject
我认为应该使用将subject
变量定义为辅助方法。这样,您就定义了一个助手方法,可以在文件中的任何地方使用它
因此,我将修改您的代码,如下所示:
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
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