Back to Repositories

Testing Gizmo Model Version Timestamp Handling in PaperTrail

This test suite examines the versioning behavior of the Gizmo model within the PaperTrail gem. It specifically focuses on timestamp handling and version creation, ensuring proper separation between model and version timestamps. The tests validate core versioning functionality while maintaining data integrity.

Test Coverage Overview

The test suite provides focused coverage of timestamp handling in the PaperTrail versioning system. Key functionality tested includes:

  • Version creation timestamp independence from model updates
  • Proper handling of touch operations during saves
  • Verification of version metadata integrity

Implementation Analysis

The testing approach uses RSpec’s describe and context blocks to organize versioning-specific test cases. It leverages RSpec’s model testing features with type: :model specification, while implementing PaperTrail’s versioning: true flag to enable version tracking during tests.

The implementation demonstrates clear separation of concerns between model timestamps and version records.

Technical Details

Testing tools and configuration:

  • RSpec as the testing framework
  • PaperTrail versioning system
  • Custom spec_helper and performance_helpers
  • Model-specific test configuration
  • Time manipulation utilities

Best Practices Demonstrated

The test suite exemplifies several testing best practices including:

  • Isolated test scenarios with clear context boundaries
  • Explicit expectations for timestamp behavior
  • Proper setup of test data with controlled timestamps
  • Clear separation between model and version concerns
  • Efficient use of RSpec’s described_class pattern

paper-trail-gem/paper_trail

spec/models/gizmo_spec.rb

            
# frozen_string_literal: true

require "spec_helper"
require "support/performance_helpers"

RSpec.describe Gizmo, type: :model, versioning: true do
  context "with a persisted record" do
    it "does not use the gizmo `updated_at` as the version's `created_at`" do
      gizmo = described_class.create(name: "Fred", created_at: Time.current - 1.day)
      gizmo.name = "Allen"
      gizmo.save(touch: false)
      expect(gizmo.versions.last.created_at).not_to(eq(gizmo.updated_at))
    end
  end
end