Outbox.Testing (Outbox v0.1.0-beta.1)

Copy Markdown View Source

Test helpers for asserting domain-event publication and exercising subscribers synchronously.

Import in your test:

defmodule MyTest do
  use ExUnit.Case
  import Outbox.Testing

  test "create_widget publishes widget.created" do
    MyContext.create_widget(...)
    assert_published("widget.created", %{"id" => "expected_id"})
  end

  test "subscriber side-effect runs end-to-end" do
    with_sync_dispatch(fn ->
      MyContext.create_widget(...)
    end)
    assert MyExternalSystem.was_called?()
  end
end

Summary

Functions

Asserts that an event with the given name was published during the test, optionally matching a subset of payload keys.

Runs the given function and synchronously dispatches every event published during it, then drains the outbox queue on the Outbox.Config.oban/0 instance so all registered subscribers run before returning.

Functions

assert_published(name, payload_match \\ %{})

@spec assert_published(String.t(), map()) :: :ok | no_return()

Asserts that an event with the given name was published during the test, optionally matching a subset of payload keys.

Examples

assert_published("product.created")
assert_published("product.created", %{"id" => "p_abc"})
assert_published("variant.updated", %{"id" => "v_xyz"})

with_sync_dispatch(fun)

@spec with_sync_dispatch((-> any())) :: any()

Runs the given function and synchronously dispatches every event published during it, then drains the outbox queue on the Outbox.Config.oban/0 instance so all registered subscribers run before returning.