Module: RailsAuditLog::Matchers

Defined in:
lib/rails_audit_log/matchers.rb

Overview

RSpec matchers for asserting audit log behaviour.

Setup

# spec/rails_helper.rb
require "rails_audit_log/matchers"

RSpec.configure do |config|
  config.include RailsAuditLog::Matchers
end

Usage

# Assert a record already has an entry
expect(post).to have_audit_log_entry(:update).touching(:title)

# Assert a block creates a new entry
expect { post.update!(title: "New") }.to create_audit_log_entry(event: :update, touching: :title)

Defined Under Namespace

Classes: CreateAuditLogEntry, HaveAuditLogEntry

Instance Method Summary collapse

Instance Method Details

#create_audit_log_entry(event: nil, touching: nil) ⇒ CreateAuditLogEntry

Returns a matcher that asserts the block creates at least one new AuditLogEntry matching the given filters.

Examples:

expect { post.update!(title: "x") }.to create_audit_log_entry(event: :update)
expect { post.update!(title: "x") }.to create_audit_log_entry(touching: :title)

Parameters:

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

    optional event filter

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

    optional attribute filter

Returns:



44
45
46
# File 'lib/rails_audit_log/matchers.rb', line 44

def create_audit_log_entry(event: nil, touching: nil)
  CreateAuditLogEntry.new(event: event, touching: touching)
end

#have_audit_log_entry(event = nil) ⇒ HaveAuditLogEntry

Returns a matcher that asserts the record has at least one matching AuditLogEntry.

Examples:

expect(post).to have_audit_log_entry
expect(post).to have_audit_log_entry(:update)
expect(post).to have_audit_log_entry(:update).touching(:title)

Parameters:

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

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

Returns:



31
32
33
# File 'lib/rails_audit_log/matchers.rb', line 31

def have_audit_log_entry(event = nil)
  HaveAuditLogEntry.new(event)
end