Mar*_*iwa 2 rspec ruby-on-rails rspec2 ruby-on-rails-4
在控制器中,我正在使用带有线的外部地理编码服务:
loc = Location.geocode(@event.raw_location)
Run Code Online (Sandbox Code Playgroud)
我想为我的所有测试设置一个存根:
allow(Location).to receive(:geocode).with(nil).and_return({city: nil, state: nil, country: nil})
Run Code Online (Sandbox Code Playgroud)
我应该把这段代码放在哪里?
您应该before(:each)在您的rails_helper.rb或spec_helper.rb
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
Run Code Online (Sandbox Code Playgroud)
编辑:
此外,如果您只想为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
Run Code Online (Sandbox Code Playgroud)
然后在你的测试中:
describe Location, geocoding_mock: true do
...
end
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
530 次 |
| 最近记录: |