Back to Repositories

Testing Method Alias Implementation in Geocoder

This test suite validates method aliases in the Geocoder gem, ensuring that alternative method names correctly map to their primary implementations. The tests verify the functionality of distance calculations, coordinate fetching, and reverse geocoding aliases.

Test Coverage Overview

The test suite provides comprehensive coverage of Geocoder’s method aliases, focusing on three core functionalities:
  • Distance calculation methods (distance_from/distance_to)
  • Coordinate fetching methods (fetch_coordinates/geocode)
  • Address retrieval methods (fetch_address/reverse_geocode)
Each test case validates that the alias method returns identical results to its primary counterpart, ensuring consistent behavior across the API.

Implementation Analysis

The testing approach employs Ruby’s unit testing framework to verify method alias functionality. The implementation uses GeocoderTestCase as the base class and creates test instances with predefined parameters. Each test method follows a clear pattern of setting up test objects, executing both alias and original methods, and asserting their equivalence.

The tests utilize helper methods like geocoded_object_params and reverse_geocoded_object_params to maintain consistent test data.

Technical Details

Testing tools and configuration:
  • Ruby unit test framework
  • Custom GeocoderTestCase test class
  • Place and PlaceReverseGeocoded model classes
  • Test helper methods for parameter generation
  • Assertion methods for equality and pattern matching

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby:
  • Isolated test cases with clear, single-responsibility focus
  • Consistent naming conventions for test methods
  • Proper use of setup helpers and test data generation
  • Explicit assertions with meaningful messages
  • Comprehensive coverage of API alternatives

alexreisner/geocoder

test/unit/method_aliases_test.rb

            
# encoding: utf-8
require 'test_helper'

class MethodAliasesTest < GeocoderTestCase

  def test_distance_from_is_alias_for_distance_to
    v = Place.new(*geocoded_object_params(:msg))
    v.latitude, v.longitude = [40.750354, -73.993371]
    assert_equal v.distance_from([30, -94]), v.distance_to([30, -94])
  end

  def test_fetch_coordinates_is_alias_for_geocode
    v = Place.new(*geocoded_object_params(:msg))
    assert_equal [Float, Float], v.fetch_coordinates.map(&:class)
  end

  def test_fetch_address_is_alias_for_reverse_geocode
    v = PlaceReverseGeocoded.new(*reverse_geocoded_object_params(:msg))
    assert_match(/New York/, v.fetch_address)
  end
end