Search code examples
ruby-on-railsfactory-botgmaps4rails

How to stub gmaps4rails geocode functions in rspec tests?


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.


Solution

  • 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