gpt4 book ai didi

ruby-on-rails - # 的 RSpec 3 未定义方法 `allow'

转载 作者:数据小太阳 更新时间:2023-10-29 07:39:22 24 4
gpt4 key购买 nike

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

我正在尝试为该主题分配一个属性...我似乎无法正确使用咒语。我这里需要双人间吗?我不确定它是如何工作的,而且我显然无法破译文档。感谢您的帮助。

最佳答案

我认为您应该使用 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

关于ruby-on-rails - #<RSpec::Core::ExampleGroup...> 的 RSpec 3 未定义方法 `allow',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26639702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com