Reputation: 5806
How can I test the fact that Geokit::Geocoders::MultiGeocoder.geocode(generate_address)
was called only after a save. Should I write my code differently?
Please, Help.
`
https://gist.github.com/bennacer860/b67583afee1bb9255938a3a621a80a54
class Playspace < ActiveRecord::Base
before_validation :geocode_address, on: [:create, :update]
def geocode_address
geo = Geokit::Geocoders::MultiGeocoder.geocode(generate_address)
# puts geo
if geo.success
self.lat = geo.lat
self.lng = geo.lng
self.state_code = geo.state_code
self.country_code = geo.country_code
self.geolocated_at = Time.now.utc
else
errors.add(:address, "Could not Geocode address #{generate_address}")
end
end
def did_address_change?
%w( address city state_code country_code postal_code lat lng ).each do |column|
return true if (self.send("#{column}_changed?"))
end
false
end
end
Upvotes: 0
Views: 76
Reputation: 24813
I would recommend mocking out your external API calls with VCR. In fact, the gem you are using in the code uses VCR for its own test suite.
Once you have VCR setup in your tests, they might contain something like this:
VCR.use_cassette("geocode") do
playspace.geocode_address
# Check that one of the columns changed
assert playspace.did_address_change?
end
Upvotes: 1