How to stub gmaps4rails geocode functions in rspec tests?

爱⌒轻易说出口 提交于 2019-12-23 09:59:49

问题


I'm using gmaps4rails, and trying to develop some tests.

I have a factory

factory :country do
   sequence(:name) { |n| "Country#{n}" }
end

which is obviously not recognized by Google.

Validation failed: Gmaps4rails address Address invalid

The API calls are also taking time to run in my tests.

How can I stub the API call out?

I've tried adding

before(:each)
  Country.stub(:geocode)
end

to the spec file but it has no effect.

My model looks like this

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

Thanks for any ideas.


回答1:


There is nothing to do with integers in the name.

I guess the address is absent or invalid.

Anyway, there are plenty of ways to either skip geocoding or stub it.

Take inspiration from the gem's specs



来源:https://stackoverflow.com/questions/10713434/how-to-stub-gmaps4rails-geocode-functions-in-rspec-tests

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!