Back to Repositories

Testing FooWidget Version Management in Paper Trail

This test suite validates the versioning functionality of the FooWidget model within PaperTrail, focusing on type preservation, version relationships, and originator tracking. The tests ensure proper version history management and object reification across model lifecycle events.

Test Coverage Overview

The test suite provides comprehensive coverage of PaperTrail’s versioning capabilities for the FooWidget model.

  • Version relationship tracking between consecutive records
  • Originator attribution verification
  • Type preservation during model reification
  • Proper handling of destroy events

Implementation Analysis

The testing approach utilizes RSpec’s context-based structure to organize related test scenarios. It leverages PaperTrail’s versioning system to track model changes and verify version chain integrity.

The implementation employs RSpec’s let blocks for test setup and before hooks to establish test state, demonstrating proper test isolation practices.

Technical Details

  • RSpec as the testing framework
  • PaperTrail gem for version tracking
  • Custom spec_helper and performance_helpers
  • Versioning enabled through RSpec metadata
  • Dynamic model type verification using described_class

Best Practices Demonstrated

The test suite exemplifies several testing best practices including proper test isolation, clear context organization, and comprehensive state verification.

  • Isolated test contexts with clear setup
  • Explicit state verification
  • Proper use of RSpec metadata
  • Consistent assertion patterns

paper-trail-gem/paper_trail

spec/models/foo_widget_spec.rb

            
# frozen_string_literal: true

require "spec_helper"
require "support/performance_helpers"

RSpec.describe(FooWidget, versioning: true) do
  context "with a subclass" do
    let(:foo) { described_class.create }

    before do
      foo.update!(name: "Foo")
    end

    it "reify with the correct type" do
      expect(PaperTrail::Version.last.previous).to(eq(foo.versions.first))
      expect(PaperTrail::Version.last.next).to(be_nil)
    end

    it "returns the correct originator" do
      PaperTrail.request.whodunnit = "Ben"
      foo.update_attribute(:name, "Geoffrey")
      expect(foo.paper_trail.originator).to(eq(PaperTrail.request.whodunnit))
    end

    context "when destroyed" do
      before { foo.destroy }

      it "reify with the correct type" do
        assert_kind_of(described_class, foo.versions.last.reify)
        expect(PaperTrail::Version.last.previous).to(eq(foo.versions[1]))
        expect(PaperTrail::Version.last.next).to(be_nil)
      end
    end
  end
end