in the city
GeoKit stubbing for faster tests
We recently added geocoding to after_save on an address model to keep track of peoples lat/lng, and found that it added substantial time to our tests. The solution? Easy.
Don’t really need to test that geolocating works, I mean.. GeoKit has its own unit tests. So Stub it! Stub it good!
Place this in your test_helper.rb (and inside Test::Unit::TestCase) for tolerable test times.
“This was written for mocha, but its easy to adapt to rspec or flexmock”
setup :stub_geocoder def stub_geocoder geocode_payload = GeoKit::GeoLoc.new(:lat => 123.456, :lng => 123.456) geocode_payload.success = true GeoKit::Geocoders::MultiGeocoder.stubs(:geocode).returns(geocode_payload) end
“Just remember to turn it off if you plan to test any features that rely on actual geocoding!”