Testing with geocoder gem
Asked Answered
C

4

9

I'm using the geocoder gem, but I don't know in which file I must paste this code. Can you tell me ?

Crumbly answered 5/9, 2013 at 15:11 Comment(0)
C
16

That piece of code should be in the SETUP portion of whatever test framework you are using.

If using rspec, it should go here:

describe Something do
 before(:all) do 
  Geocoder.configure(:lookup => :test)

  Geocoder::Lookup::Test.add_stub(
  "New York, NY", [
    {
      'latitude'     => 40.7143528,
      'longitude'    => -74.0059731,
      'address'      => 'New York, NY, USA',
      'state'        => 'New York',
      'state_code'   => 'NY',
      'country'      => 'United States',
      'country_code' => 'US'
    }
   ]
  )
 end
end
Chappie answered 5/9, 2013 at 15:15 Comment(2)
I just updated the answer as per your feedback, have a good oneChappie
perhaps due to a version change, but seems like the longitude/latitude hash keys were replaced with 'coordinates' => [40.7143528, -74.0059731]Britanybritches
O
11

Since you did not state your test framework, I'll give a specific answer.

I am using Cucumber and Rspec. While all of the above is true from @DevDude and @malandrina, here is a more complete tip for where the code can go, and how to also add entries for reverse geocoding (lat/lon --> address):

Put your stubs in the spec folder. I created an array of arrays so that I could add multiple "lookups" to be stubbed out:

spec/support/geocoder_stubs.rb
addresses = {
  "230 West 43rd St., New York City, NY 10036" => {
      'latitude' => 40.7573862,
      'longitude' => -73.9881256,
      'address' => '230 West 43rd St., New York City, NY 10036',
      'city' => 'New York City',
      'state' => 'New York',
      'state_code' => 'NY',
      'country' => 'United States',
      'country_code' => 'US'
  },
  [40.75747130000001, -73.9877319] => {
      'latitude' => 40.75747130000001,
      'longitude' => -73.9877319,
      'address' => '229 West 43rd St., New York City, NY 10036',
      'city' => 'New York City',
      'state' => 'New York',
      'state_code' => 'NY',
      'country' => 'United States',
      'country_code' => 'US'
  },
  "Worthington, OH" => {
    'latitude' => 40.09846115112305,
    'longitude' => -83.01747131347656,
    'address' => 'Worthington, OH',
    'city' => 'Worthington',
    'state' => 'Ohio',
    'state_code' => 'OH',
    'country' => 'United States',
    'country_code' => 'US'
  },
}

Geocoder.configure(:lookup => :test)
addresses.each { |lookup, results| Geocoder::Lookup::Test.add_stub(lookup, [results]) }

Reference your stubs in the Cucumber support folder:

features/support/env.rb
require Rails.root.join("spec/support/geocoder_stubs")

Hope this helps!

Odelia answered 30/12, 2014 at 1:20 Comment(0)
U
3

An alternative to putting your stubs in the test setup is to define them in spec/support:

spec/support/geocoder.rb

Geocoder.configure(lookup: :test)

Geocoder::Lookup::Test.add_stub(
  ...
)
end

While this approach has the downside of introducing mystery guests into your tests, it does DRY things up.

Unkind answered 24/10, 2013 at 18:50 Comment(1)
Don't forget to wrap this in module with a name of your choice, if you are including it in spec/rails_helper.rb using config.include yourModuleNameHorsehair
A
3

I am putting this code in my /config/initializers/geocoder.rb with a conditional for Rails.env.test? . I tried above approaches mentioned by @devDude , it worked out great , but i just didn't wanted any geocoding real calls being made from my rspec tests even by mistake( had a lot of specs dependent on this in a lot of files) + this approach will work for any kind of testing framework( be it testunit or mintests or with cucumber also).

This is how my /config/initializers/geocoder.rb file looks like.

if Rails.env.test?
  Geocoder.configure(:lookup => :test)
  # Particular Look up
  Geocoder::Lookup::Test.add_stub(
    "New York, NY", [
      {
        'latitude'     => 40.7143528,
        'longitude'    => -74.0059731,
        'address'      => 'New York, NY, USA',
        'state'        => 'New York',
        'state_code'   => 'NY',
        'country'      => 'United States',
        'country_code' => 'US'
      }
    ]
  )
  #default stub
  Geocoder::Lookup::Test.set_default_stub(
    [
      {
        'latitude'     => 40.7143528,
        'longitude'    => -74.0059731,
        'address'      => 'New York, NY, USA',
        'state'        => 'New York',
        'state_code'   => 'NY',
        'country'      => 'United States',
        'country_code' => 'US'
      }
    ]
  )  
else
  Geocoder.configure(
    :timeout      => 3,           # geocoding service timeout (secs)
    :lookup       => :google,     # name of geocoding service (symbol)
    :language     => :en,         # ISO-639 language code
    :units     => :mi,       # :km for kilometers or :mi for miles
    :distances => :linear    # :spherical or :linear
  )
end
Avraham answered 24/1, 2016 at 9:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.