作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
在 Controller 中,我正在使用外部地理编码服务:
loc = Location.geocode(@event.raw_location)
我想为我的所有测试设置一个 stub :
allow(Location).to receive(:geocode).with(nil).and_return({city: nil, state: nil, country: nil})
我应该把这段代码放在哪里?
最佳答案
您应该在 rails_helper.rb
或 spec_helper.rb
中声明一个全局 before(:each)
RSpec.configure do |config|
config.before(:each) do
allow(Location).to receive(:geocode).with(nil).and_return({city: nil, state: nil, country: nil})
end
end
编辑:
此外,如果您只想在涉及地理编码调用的测试中运行此“全局”before(:each)
,您可以编写:
RSpec.configure do |config|
config.before(:each, geocoding_mock: true) do
allow(Location).to receive(:geocode).with(nil).and_return({city: nil, state: nil, country: nil})
end
end
然后在你的测试中:
describe Location, geocoding_mock: true do
...
end
关于ruby-on-rails - 如何为所有测试设置 stub ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29165788/
我是一名优秀的程序员,十分优秀!