Test Geocoding with Stubs
masterTo avoid network calls during testing, configure the :test lookup and provide stubs for expected queries.
Setup:
- Set
lookup: :testinGeocoder.configure. - Use
Geocoder::Lookup::Test.add_stub(query_string, results_array)to define behavior. - Use
Geocoder::Lookup::Test.set_default_stub(results_array)for a fallback.
Note: Stub keys must be strings, not symbols.
Geocoder.configure(lookup: :test, ip_lookup: :test)
Geocoder::Lookup::Test.add_stub(
"New York, NY", [
{
'coordinates' => [40.7143528, -74.0059731],
'address' => 'New York, NY, USA',
'city' => 'New York',
'state' => 'New York',
'state_code' => 'NY',
'country' => 'United States',
'country_code' => 'US'
}
]
)