如何在rspec测试中存根gmaps4rails地理编码功能? [英] How to stub gmaps4rails geocode functions in rspec tests?
本文介绍了如何在rspec测试中存根gmaps4rails地理编码功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用gmaps4rails,并尝试开发一些测试.
I'm using gmaps4rails, and trying to develop some tests.
我有一家工厂
factory :country do
sequence(:name) { |n| "Country#{n}" }
end
这显然未被Google识别.
which is obviously not recognized by Google.
Validation failed: Gmaps4rails address Address invalid
API调用也需要一些时间才能在我的测试中运行.
The API calls are also taking time to run in my tests.
如何对API调用进行存根处理?
How can I stub the API call out?
我尝试添加
before(:each)
Country.stub(:geocode)
end
到规格文件,但无效.
我的模特看起来像这样
class Country < ActiveRecord::Base
acts_as_gmappable :lat => 'latitude', :lng => 'longitude'
def gmaps4rails_address
"#{self.name}"
end
geocoded_by :gmaps4rails_address
after_validation :geocode
end
感谢任何想法.
推荐答案
名称中的整数与无关.
我猜地址不存在或无效.
I guess the address is absent or invalid.
无论如何,有很多方法可以跳过地理编码或将其存根.
Anyway, there are plenty of ways to either skip geocoding or stub it.
从 gem的规范中获取灵感
Take inspiration from the gem's specs
这篇关于如何在rspec测试中存根gmaps4rails地理编码功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文