Search code examples
ruby-on-railsgoogle-mapsruby-on-rails-4rails-geocoder

Rails 4 - Geocoder hitting query limit in test suite


When running my test suite, about half the time it will go red because I am hitting the geocoding per second query limit:

Google Geocoding API error: over query limit.    

I am using the geocoder gem with google maps / places api.

It seems like I need to set some sort of sleep option, but in the geocoder documentation, the only mention of this is for a rake task. How can I set it up, or prevent my test suite from making too many geocode requests per second?


Solution

  • While not specific to the geocoder gem, your best bet is probably stubbing out the service somehow.

    You have a few options:

    1. You could turn on caching in your test environment so you only make one external http request per day. Rails Guides has some good examples of ActiveSupport::Cache. The relevant section is low-level caching. While this may work for you, I think its a bit overly-complicated for your situation.

    2. I recommend mocking out the external HTTP service all together. Thoughtbot has a somewhat outdated but still relevant guide to setting up Webmock, a popular library for mocking out external HTTP services. It integrates well with all of the popular testing frameworks.

    Hope this helps.