diff --git a/README.md b/README.md index d84dc34a92d4e22f426bfecdf04a8d5644d35e67..c65a850d432893fb5d28214c995da7c4ac31898f 100644 --- a/README.md +++ b/README.md @@ -538,8 +538,25 @@ When writing tests for an app that uses Geocoder it may be useful to avoid netwo ] ) -Now, any time Geocoder looks up "New York, NY" its results array will contain one result with the above attributes. +Now, any time Geocoder looks up "New York, NY" its results array will contain one result with the above attributes. You can also set a default stub: + Geocoder.configure(:lookup => :test) + + 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' + } + ] + ) + +Any query that hasn't been explicitly stubbed will return that result. Command Line Interface ---------------------- diff --git a/lib/geocoder/lookups/test.rb b/lib/geocoder/lookups/test.rb index ebf7b5d76a2d201aeb39599826d0b1100ce22fd0..1a2d0cf6998f8f31050b7c0fa8bb8b162c450cd5 100644 --- a/lib/geocoder/lookups/test.rb +++ b/lib/geocoder/lookups/test.rb @@ -13,8 +13,13 @@ module Geocoder stubs[query_text] = results end + def self.set_default_stub(results) + @default_stub = results + end + def self.read_stub(query_text) stubs.fetch(query_text) { + return @default_stub unless @default_stub.nil? raise ArgumentError, "unknown stub request #{query_text}" } end @@ -25,6 +30,7 @@ module Geocoder def self.reset @stubs = {} + @default_stub = nil end private diff --git a/test/test_mode_test.rb b/test/test_mode_test.rb index 9e80726a29f32fd81db741af141ecdf1b7569e40..7f4b611581829c59fbf8ba3ae57f76a38d9d04de 100644 --- a/test/test_mode_test.rb +++ b/test/test_mode_test.rb @@ -13,8 +13,39 @@ class TestModeTest < Test::Unit::TestCase end def test_search_with_known_stub + Geocoder::Lookup::Test.add_stub("New York, NY", [mock_attributes]) + + results = Geocoder.search("New York, NY") + result = results.first + + assert_equal 1, results.size + mock_attributes.keys.each do |attr| + assert_equal mock_attributes[attr], result.send(attr) + end + end + + def test_search_with_unknown_stub_without_default + assert_raise ArgumentError do + Geocoder.search("New York, NY") + end + end + + def test_search_with_unknown_stub_with_default + Geocoder::Lookup::Test.set_default_stub([mock_attributes]) + + results = Geocoder.search("Atlantis, OC") + result = results.first + + assert_equal 1, results.size + mock_attributes.keys.each do |attr| + assert_equal mock_attributes[attr], result.send(attr) + end + end + + private + def mock_attributes coordinates = [40.7143528, -74.0059731] - attributes = { + @mock_attributes ||= { 'coordinates' => coordinates, 'latitude' => coordinates[0], 'longitude' => coordinates[1], @@ -24,27 +55,5 @@ class TestModeTest < Test::Unit::TestCase 'country' => 'United States', 'country_code' => 'US', } - - Geocoder::Lookup::Test.add_stub("New York, NY", [attributes]) - - results = Geocoder.search("New York, NY") - assert_equal 1, results.size - - result = results.first - assert_equal coordinates, result.coordinates - assert_equal attributes['latitude'], result.latitude - assert_equal attributes['longitude'], result.longitude - assert_equal attributes['address'], result.address - assert_equal attributes['state'], result.state - assert_equal attributes['state_code'], result.state_code - assert_equal attributes['country'], result.country - assert_equal attributes['country_code'], result.country_code - end - - def test_search_with_unknown_stub - assert_raise ArgumentError do - Geocoder.search("New York, NY") - end end - end