Back to Repositories

Testing NotOnUpdate Version Management in PaperTrail

This test suite examines the behavior of the NotOnUpdate model in PaperTrail, specifically focusing on version creation and change tracking. The tests verify the save_with_version functionality and its interaction with callback states, ensuring proper version management and change capture.

Test Coverage Overview

The test suite provides comprehensive coverage of the NotOnUpdate model’s versioning capabilities.

Key areas tested include:
  • Version creation through save_with_version method
  • Change tracking during callback states
  • Version count verification
  • Changeset capture accuracy

Implementation Analysis

The testing approach utilizes RSpec’s describe and context blocks to organize test scenarios logically. The implementation leverages let blocks for test setup and expectations to verify version creation and change tracking. The tests specifically focus on the paper_trail integration points and version management behavior.

Technical Details

Testing tools and configuration:
  • RSpec as the testing framework
  • PaperTrail gem integration
  • Model-level versioning enabled through ‘versioning: true’
  • Frozen string literal pragma
  • Custom model setup with version tracking

Best Practices Demonstrated

The test suite exemplifies several testing best practices:

  • Isolated test scenarios with clear setup and expectations
  • Proper use of let blocks for test data preparation
  • Explicit version count verification
  • Specific changeset validation
  • Clear test case organization and naming

paper-trail-gem/paper_trail

spec/models/not_on_update_spec.rb

            
# frozen_string_literal: true

require "spec_helper"

RSpec.describe NotOnUpdate, type: :model do
  describe "#save_with_version", versioning: true do
    let!(:record) { described_class.create! }

    it "creates a version, regardless" do
      expect { record.paper_trail.save_with_version }.to change {
        PaperTrail::Version.count
      }.by(+1)
    end

    it "captures changes when in_after_callback is true" do
      record.name = "test"
      record.paper_trail.save_with_version(in_after_callback: true)
      changeset = record.versions.last.changeset
      expect(changeset[:name]).to eq([nil, "test"])
    end
  end
end