From 45353b00338a89d37d151cdca98b6308e421c796 Mon Sep 17 00:00:00 2001
From: ian asaff <ian.asaff@gmail.com>
Date: Wed, 3 Apr 2013 18:06:57 -0400
Subject: [PATCH] add support for setting a default stub in test mode. updated
 readme to reflect addition.

---
 README.md                    | 19 ++++++++++++-
 lib/geocoder/lookups/test.rb |  6 ++++
 test/test_mode_test.rb       | 55 +++++++++++++++++++++---------------
 3 files changed, 56 insertions(+), 24 deletions(-)

diff --git a/README.md b/README.md
index 598d0259..ed4cc550 100644
--- a/README.md
+++ b/README.md
@@ -514,8 +514,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 ebf7b5d7..1a2d0cf6 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 9e80726a..7f4b6115 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
-- 
GitLab