Back to Repositories

Validating Test Mode Implementation in geocoder

This test suite validates the test mode functionality in the Geocoder gem, ensuring proper handling of geocoding stubs and mock data for testing purposes.

Test Coverage Overview

The test suite provides comprehensive coverage of Geocoder’s test mode functionality.

Key areas tested include:
  • Stub creation and validation with known locations
  • Default stub behavior
  • Custom attribute handling
  • Invalid address scenarios
  • Stub removal and cleanup

Implementation Analysis

The testing approach uses Ruby’s Test::Unit framework with a focus on isolated test mode verification.

Key implementation patterns include:
  • Setup/teardown hooks for test isolation
  • Mock attribute management
  • Exception handling validation
  • Attribute verification loops

Technical Details

Testing infrastructure includes:
  • GeocoderTestCase as the base test class
  • Test::Unit framework integration
  • Mock attribute fixtures
  • Configuration management through Geocoder.configure
  • Lookup::Test module for stub management

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Proper test isolation through setup/teardown
  • Comprehensive edge case coverage
  • Clear test method naming
  • DRY principles via shared mock attributes
  • Explicit assertion messages

alexreisner/geocoder

test/unit/test_mode_test.rb

            
# encoding: utf-8
require 'test_helper'

class TestModeTest < GeocoderTestCase

  def setup
    @_original_lookup = Geocoder.config.lookup
    Geocoder.configure(:lookup => :test)
  end

  def teardown
    Geocoder::Lookup::Test.reset
    Geocoder.configure(:lookup => @_original_lookup)
  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.each_key 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

  def test_search_with_custom_attributes
    custom_attributes = mock_attributes.merge(:custom => 'NY, NY')
    Geocoder::Lookup::Test.add_stub("New York, NY", [custom_attributes])

    result = Geocoder.search("New York, NY").first

    assert_equal 'NY, NY', result.custom
  end

  def test_search_with_invalid_address_stub
    Geocoder::Lookup::Test.add_stub("invalid address/no result", [])

    result = Geocoder.search("invalid address/no result")

    assert_equal [], result
  end

  def test_unsetting_stub
    Geocoder::Lookup::Test.add_stub("New York, NY", [mock_attributes])

    assert_nothing_raised ArgumentError do
      Geocoder.search("New York, NY")
    end

    Geocoder::Lookup::Test.delete_stub("New York, NY")

    assert_raise ArgumentError do
      Geocoder.search("New York, NY")
    end
  end

  private
  def mock_attributes
    coordinates = [40.7143528, -74.0059731]
    @mock_attributes ||= {
      'coordinates'  => coordinates,
      'latitude'     => coordinates[0],
      'longitude'    => coordinates[1],
      'address'      => 'New York, NY, USA',
      'state'        => 'New York',
      'state_code'   => 'NY',
      'country'      => 'United States',
      'country_code' => 'US',
    }
  end
end