Back to Repositories

Testing FriendlyId Finder Methods in norman/friendly_id

This test suite validates the finder functionality in FriendlyId, focusing on how the library handles record lookup using friendly URLs and slugs. It thoroughly tests the integration between ActiveRecord models and FriendlyId’s finder methods, particularly examining nil handling and relation-based queries.

Test Coverage Overview

The test suite provides comprehensive coverage of FriendlyId’s finder capabilities, focusing on both direct model queries and scoped relations.

  • Tests basic record finding using friendly IDs
  • Validates finder behavior within ActiveRecord scopes
  • Extensive nil handling scenarios with allow_nil option
  • Edge cases with invalid primary keys and IDs

Implementation Analysis

The testing approach employs Minitest’s assertion-based methodology with a custom TestCaseClass implementation. The suite uses a mock Journalist model to demonstrate FriendlyId integration.

  • Utilizes helper methods for instance creation
  • Implements scoped queries through ActiveRecord relations
  • Leverages FriendlyId’s slugged and finders modules

Technical Details

  • Testing Framework: Minitest
  • ORM: ActiveRecord
  • Custom Helpers: FriendlyId::Test module
  • Model Configuration: Uses slugged and finders features
  • Database: Custom table configuration for journalists

Best Practices Demonstrated

The test suite exemplifies robust testing practices through systematic coverage of functionality variations.

  • Consistent test naming conventions
  • Isolated test cases with proper setup
  • Comprehensive edge case handling
  • DRY implementation using helper methods
  • Clear separation of concerns between model and test logic

norman/friendly_id

test/finders_test.rb

            
require "helper"

class JournalistWithFriendlyFinders < ActiveRecord::Base
  self.table_name = "journalists"
  extend FriendlyId
  scope :existing, -> { where("1 = 1") }
  friendly_id :name, use: [:slugged, :finders]
end

class Finders < TestCaseClass
  include FriendlyId::Test

  def model_class
    JournalistWithFriendlyFinders
  end

  test "should find records with finders as class methods" do
    with_instance_of(model_class) do |record|
      assert model_class.find(record.friendly_id)
    end
  end

  test "should find records with finders on relations" do
    with_instance_of(model_class) do |record|
      assert model_class.existing.find(record.friendly_id)
    end
  end

  test "allows nil with allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.find("foo", allow_nil: true)
    end
  end

  test "allows nil on relations with allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.existing.find("foo", allow_nil: true)
    end
  end

  test "allows nil with a bad primary key ID and allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.find(0, allow_nil: true)
    end
  end

  test "allows nil on relations with a bad primary key ID and allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.existing.find(0, allow_nil: true)
    end
  end

  test "allows nil with a bad potential primary key ID and allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.find("0", allow_nil: true)
    end
  end

  test "allows nil on relations with a bad potential primary key ID and allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.existing.find("0", allow_nil: true)
    end
  end

  test "allows nil with nil ID and allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.find(nil, allow_nil: true)
    end
  end

  test "allows nil on relations with nil ID and allow_nil: true" do
    with_instance_of(model_class) do |record|
      assert_nil model_class.existing.find(nil, allow_nil: true)
    end
  end
end