Back to Repositories

Testing Plant Model Versioning and STI Implementation in Paper Trail

This test suite validates the Paper Trail versioning functionality for the Plant model, focusing on Single Table Inheritance (STI) implementation and version restoration. The tests ensure proper model versioning and handling of non-standard STI column contents across different plant types.

Test Coverage Overview

The test suite provides comprehensive coverage of Plant model versioning functionality with STI:

  • Baseline versioning configuration verification
  • STI inheritance column validation
  • Version restoration with non-standard STI implementations
  • Cross-model version handling between Plant and Tomato classes

Implementation Analysis

The testing approach employs RSpec’s model specs with Paper Trail versioning enabled. It validates both standard model functionality and STI-specific behavior using described_class for flexible test implementations. The suite demonstrates proper handling of model inheritance and version restoration across different model types.

Technical Details

Testing tools and configuration:

  • RSpec framework with model specs
  • Paper Trail versioning configuration
  • STI implementation with custom species column
  • Version reification testing
  • Model destruction and recreation verification

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases with clear assertions
  • Proper setup of versioning configuration
  • Comprehensive STI implementation testing
  • Explicit class type verification
  • Clean test organization with descriptive contexts

paper-trail-gem/paper_trail

spec/models/plant_spec.rb

            
# frozen_string_literal: true

require "spec_helper"

RSpec.describe Plant, type: :model, versioning: true do
  it "baseline test setup" do
    expect(described_class.new).to be_versioned
    expect(described_class.inheritance_column).to eq("species")
  end

  describe "#descends_from_active_record?" do
    it "returns true, meaning that Animal is not an STI subclass" do
      expect(described_class.descends_from_active_record?).to eq(true)
    end
  end

  it "works with non standard STI column contents" do
    plant = described_class.create
    plant.destroy

    tomato = Tomato.create
    tomato.destroy

    reified = plant.versions.last.reify
    expect(reified.class).to eq(described_class)

    reified = tomato.versions.last.reify
    expect(reified.class).to eq(Tomato)
  end
end