5

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.

Andy Harvey
  • 12,333
  • 17
  • 93
  • 185

1 Answers1

5

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

apneadiving
  • 114,565
  • 26
  • 219
  • 213
  • Hi apneadiving. thanks for the answer. Seems that the invalid address is a response from Google. Does this sound right? So how do I stub geocoding? I'm running a single test country_specs.rb, in which I have a before :each block that contains `Country.stub(:geocode)`. I've tried a few other things `.stub_geocode`, `.stub(:gmaps4rails)`, etc. But geocoding is still being run. I'll add my model code to the question. Appreciate any suggestions you have – Andy Harvey May 24 '12 at 09:13
  • it comes from the gem. For stubbing look here: https://github.com/apneadiving/Google-Maps-for-Rails/blob/master/spec/support/geocoding.rb – apneadiving May 24 '12 at 11:15
  • actually I spoke too soon. I'm still seeing API calls in my test. I've tried adding `Geocoding.stub_gecoding` and `Gmaps4rails.stub!(:geocode).and_return [mock_address]` Scratching my head! Is there any documentation on testing with gmaps-4-rails that I've overlooked? – Andy Harvey May 24 '12 at 14:06
  • `Gmaps4rails.stub(:geocode)` should be enough. Again loik at my specs, there is nothing more and it works fine – apneadiving May 24 '12 at 14:27
  • ok, thanks for the confirmation. I thought it should work. I must be doing something wrong elsewhere. Will keep digging :) Thanks for the help, and a great gem – Andy Harvey May 24 '12 at 14:36
  • just to close this, I finally tracked down my issue. I was trying `Gmaps4rails.stub(:geocode).and_return [stuff]` but still getting API calls. I'm in the process of moving from geocoder to gmaps4rails, and had overlooked some old methods. Geocoder was to blame (well, me really). Thanks again, sorry for the trouble. – Andy Harvey May 25 '12 at 09:19
  • As I look at those specs, it appears that you set the options at the begining of the spec file; but is there any way to disable geocoding for *all* specs, unless explicitly requested? Sort of like the "no_peeping_toms" gem for observers? – DGM Jul 27 '12 at 16:23
  • @DGM: nope I didn't code anything in this scope. You can disable it if you pass `false` to `process_geocoding` which could be a lambda or method configured conditionnaly to Rails env – apneadiving Jul 27 '12 at 16:44
  • Oh, in the model, pass false in test environnment? Or perhaps have test environment monkey patch it on load? – DGM Jul 27 '12 at 16:46