Module: RailsAuditLog::MinitestAssertions

Defined in:
lib/rails_audit_log/minitest_assertions.rb

Overview

Opt-in Minitest assertions for audit log expectations.

Setup

# test/test_helper.rb
require "rails_audit_log/minitest_assertions"

class ActiveSupport::TestCase
  include RailsAuditLog::MinitestAssertions
end

Usage

assert_audit_log_entry(post, event: :update, touching: :title)
refute_audit_log_entry(post, event: :create)

Instance Method Summary collapse

Instance Method Details

#assert_audit_log_entry(record, event: nil, touching: nil, message: nil) ⇒ void

This method returns an undefined value.

Asserts that record has at least one AuditLogEntry matching the given filters. Fails with a descriptive message when no entry is found.

Examples:

assert_audit_log_entry(post, event: :update, touching: :title)

Parameters:

  • record (ActiveRecord::Base)

    the audited record to check

  • event (Symbol, String, nil) (defaults to: nil)

    optional event filter (:create, :update, or :destroy)

  • touching (Symbol, String, nil) (defaults to: nil)

    optional attribute name filter

  • message (String, nil) (defaults to: nil)

    custom failure message; auto-generated when nil



29
30
31
32
33
# File 'lib/rails_audit_log/minitest_assertions.rb', line 29

def assert_audit_log_entry(record, event: nil, touching: nil, message: nil)
  scope = build_scope(record, event, touching)
  msg   = message || default_message("to have", record, event, touching)
  assert scope.exists?, msg
end

#refute_audit_log_entry(record, event: nil, touching: nil, message: nil) ⇒ void

This method returns an undefined value.

Asserts that record has no AuditLogEntry matching the given filters. Fails with a descriptive message when a matching entry is found.

Examples:

refute_audit_log_entry(post, event: :destroy)

Parameters:

  • record (ActiveRecord::Base)

    the audited record to check

  • event (Symbol, String, nil) (defaults to: nil)

    optional event filter

  • touching (Symbol, String, nil) (defaults to: nil)

    optional attribute name filter

  • message (String, nil) (defaults to: nil)

    custom failure message; auto-generated when nil



45
46
47
48
49
# File 'lib/rails_audit_log/minitest_assertions.rb', line 45

def refute_audit_log_entry(record, event: nil, touching: nil, message: nil)
  scope = build_scope(record, event, touching)
  msg   = message || default_message("not to have", record, event, touching)
  refute scope.exists?, msg
end