Best approach in testing geocoder gem with RSpec

1.2k views Asked by At

I know this has been asked before but since a newer version came out and since the last relevant post was dated 2 years ago, I was wondering to those who use this and follow TDD to the T, what's your approach in testing all of it's functionality (geocode, near, within_bounding_box, distance, etc).

Currently, there is a method for doing that.

My question is, how would you stub multiple results? Because you'd need multiple data for testing near, within_bounding_box, distance or other calculated functionality.

Thanks for your time reading this and any feedback that may come!

1

There are 1 answers

0
Wes Foster On

You would simply pass more than one address into the stub and call it before your tests:

# Create your list of addresses
addresses = [
  ["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'
  }]],
  ["Clarksville, TN", [{
    'latitude' => 36.4722803,
    'longitude' => -87.3612205,
    'address' => 'Clarksville, TN 37040, USA',
    'state' => 'Tennessee',
    'state_code' => 'TN',
    'country' => 'United States',
    'country_code' => 'US'
  }]],
]

# Include this in a before(:all) method before your tests
Geocoder.configure( :lookup => :test )
addresses.each { |address| Geocoder::Lookup::Test.add_stub(address[0], address[1])}