Geocoding Testing

I am using a geocoder gem, but I do not know in which file I should paste this code . Can you tell me

+6
source share
4 answers

This piece of code should be in the SETUP part of any testing environment you use.

If you are 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 
+14
source

Since you did not specify your test structure, I will give a specific answer.

I use Cucumber and Rspec. Although all of the above is true from @DevDude and @malandrina, here is a more complete tip on which code can go, and how to add entries for reverse geocoding (lat / lon β†’ address):

Put your stubs in the spec folder. I created an array of arrays so that I can add some β€œlookups” that need to be flashed:

 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]) } 

Link to your stubs in the cucumber support folder:

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

Hope this helps!

+7
source

An alternative to putting your stubs in a test setup is defining them in spec/support :

Specification / Support / geocoder.rb

 Geocoder.configure(lookup: :test) Geocoder::Lookup::Test.add_stub( ... ) end 

Although this approach has the disadvantage of introducing mysterious guests into its tests, it does DRY stuff.

+3
source

Am I putting this code in my /config/initializers/geocoder.rb with a conditional value for Rails.env.test? . I tried the above approaches mentioned by @devDude, it turned out great, but I just didn’t want the real geocoding calls to be made from my rspec tests even by mistake (I had many specifications that depend on this in many files) + this approach will work for any kind of testing framework (be it testunit or mintests or with cucumber).

This is what 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 
+1
source

Source: https://habr.com/ru/post/953249/


All Articles