Skip to content

Instantly share code, notes, and snippets.

@dinks
Created June 5, 2015 20:06
Show Gist options
  • Save dinks/2e8817edc11810babf5b to your computer and use it in GitHub Desktop.
Save dinks/2e8817edc11810babf5b to your computer and use it in GitHub Desktop.
Mock Geocoder
# /spec/support/mock_geocoder.rb
# Used with ffaker
RSpec.configure do |c|
class MockResult < ::Geocoder::Result::Base
def initialize(data = [])
super(data)
end
end
c.before(:each, :mock_geocoder) do
options = {
address: FFaker::AddressAU.full_address,
coordinates: [FFaker::Geolocation.lat, FFaker::Geolocation.lng],
state: FFaker::AddressAU.state,
state_code: FFaker::AddressAU.state_abbr,
country: FFaker::AddressAU.country,
country_code: FFaker::AddressAU.country_code
}
MockResult.new.tap do |result|
options.each do |prop, val|
allow(result).to receive(prop).and_return(val)
end
allow(Geocoder).to receive(:search).and_return([result])
end
end
c.after(:each, :mock_geocoder) do
# Nothing to do
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment