AuditTrail.Test (audit_trail v0.1.0)

Copy Markdown

Test helpers for asserting on audit events, meant to be used together with AuditTrail.Adapters.Test (see its moduledoc for setup).

test "approving an item logs an audit event" do
  AuditTrail.Test.clear()
  {:ok, item} = Items.approve(item_id)

  AuditTrail.Test.assert_logged("item:approved", %{resource_id: item.id})
end

Matching is done against the raw, atom-keyed log map (as built by AuditTrail.Logger) — top-level fields like :resource_id/:status, or anything nested under :meta, e.g. %{meta: %{changes: %{status: %{to: "approved"}}}}.

Summary

Functions

Asserts an event matching event_type (and optionally a map of expected fields, checked recursively) was logged. Polls for up to 200ms since logging happens asynchronously. Returns the first matching log.

Asserts no event matching event_type (and optional expected fields) was logged. Waits out the same poll window as assert_logged/2 so it isn't fooled by a log that just hasn't been flushed yet — call this after whatever action you expect to (not) produce the event.

Clears every captured log. Call in setup to isolate tests.

All logs captured so far, oldest first.

Functions

assert_logged(event_type, expected \\ %{})

Asserts an event matching event_type (and optionally a map of expected fields, checked recursively) was logged. Polls for up to 200ms since logging happens asynchronously. Returns the first matching log.

assert_not_logged(event_type, expected \\ %{})

Asserts no event matching event_type (and optional expected fields) was logged. Waits out the same poll window as assert_logged/2 so it isn't fooled by a log that just hasn't been flushed yet — call this after whatever action you expect to (not) produce the event.

clear()

Clears every captured log. Call in setup to isolate tests.

logs()

All logs captured so far, oldest first.