Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 573
Andy Harvey Avatar asked May 23 '12 04:05

Andy Harvey


1 Answers

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

like image 143
apneadiving Avatar answered Sep 23 '22 19:09

apneadiving