How to stub gmaps4rails geocode functions in rspec

2019-06-23 03:57发布

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条回答
放荡不羁爱自由
2楼-- · 2019-06-23 04:49

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

查看更多
登录 后发表回答