Back to Repositories

Testing FriendlyId Core Model Integration in friendly_id

This test suite validates core functionality of the FriendlyId gem, focusing on model integration and friendly URL slug generation. It ensures proper configuration and behavior of friendly identifiers in ActiveRecord models.

Test Coverage Overview

The test suite provides comprehensive coverage of FriendlyId’s core functionality, focusing on model integration and configuration.

  • Validates default behavior of models without FriendlyId
  • Tests proper configuration of friendly_id in model classes
  • Verifies friendly identifier generation for model instances
  • Covers both Book and Author model relationships

Implementation Analysis

The testing approach utilizes Minitest’s structured framework with TestCaseClass inheritance and module inclusion patterns.

Key implementation aspects include:
  • Module inclusion for shared test behaviors
  • Dynamic model class specification through model_class method
  • Instance-based testing using with_instance_of helper

Technical Details

Testing infrastructure includes:
  • Minitest as the testing framework
  • ActiveRecord integration for model testing
  • FriendlyId::Test module for shared test utilities
  • Custom test helpers for instance management
  • Abstract class testing capabilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices in Ruby.

  • Modular test organization using shared behaviors
  • Isolated test cases with clear assertions
  • Proper setup of model relationships
  • Comprehensive edge case coverage
  • Clear test naming conventions

norman/friendly_id

test/core_test.rb

            
require "helper"

class Book < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name
end

class Author < ActiveRecord::Base
  extend FriendlyId
  friendly_id :name
  has_many :books
end

class CoreTest < TestCaseClass
  include FriendlyId::Test
  include FriendlyId::Test::Shared::Core

  def model_class
    Author
  end

  test "models don't use friendly_id by default" do
    assert !Class.new(ActiveRecord::Base) {
      self.abstract_class = true
    }.respond_to?(:friendly_id)
  end

  test "model classes should have a friendly id config" do
    assert model_class.friendly_id(:name).friendly_id_config
  end

  test "instances should have a friendly id" do
    with_instance_of(model_class) { |record| assert record.friendly_id }
  end
end