Back to Repositories

Testing Legacy Widget Version Management in PaperTrail

This test suite validates the versioning functionality of LegacyWidget models in the PaperTrail gem. It focuses on custom version handling, version retrieval, and attribute updates while ensuring proper version tracking behavior.

Test Coverage Overview

The test suite provides comprehensive coverage of LegacyWidget versioning capabilities:

  • Custom version association verification
  • Previous version retrieval functionality
  • Version attribute handling
  • Update behavior with ignored columns

Implementation Analysis

The testing approach utilizes RSpec’s describe/it blocks for organized test cases. It implements factory-style object creation and leverages PaperTrail’s versioning functionality to track model changes. The tests demonstrate proper use of version reification and attribute manipulation.

Technical Details

Key technical components include:

  • RSpec testing framework
  • PaperTrail versioning system
  • Model-level versioning configuration
  • Custom version attribute handling
  • Version reification methods

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test cases for specific functionality
  • Clear test descriptions and expectations
  • Proper setup and state management
  • Effective use of RSpec matchers
  • Comprehensive edge case coverage

paper-trail-gem/paper_trail

spec/models/legacy_widget_spec.rb

            
# frozen_string_literal: true

require "spec_helper"

RSpec.describe LegacyWidget, type: :model, versioning: true do
  describe "#custom_version" do
    it "knows which version it came from" do
      widget = described_class.create(name: "foo", version: 2)
      %w[bar baz].each { |name| widget.update(name: name) }
      version = widget.versions.last
      reified = version.reify
      expect(reified.custom_version).to(eq(version))
    end
  end

  describe "#previous_version" do
    it "return its previous self" do
      widget = described_class.create(name: "foo", version: 2)
      %w[bar baz].each { |name| widget.update(name: name) }
      version = widget.versions.last
      reified = version.reify
      expect(reified.paper_trail.previous_version).to(eq(reified.versions[-2].reify))
    end
  end

  describe "#update" do
    it "does not create a PT version record because the updated column is ignored" do
      described_class.create.update(version: 1)
      expect(PaperTrail::Version.count).to(eq(1))
    end
  end

  describe "#version" do
    it "is a normal attribute and has nothing to do with PT" do
      widget = described_class.create(name: "foo", version: 2)
      expect(widget.versions.size).to(eq(1))
      expect(widget.version).to(eq(2))
      widget.update(version: 3)
      expect(widget.version).to(eq(3))
    end
  end
end