# `Commanded.Assertions.EventAssertions`
[🔗](https://github.com/commanded/commanded/blob/v1.4.12/lib/commanded/assertions/event_assertions.ex#L1)

Provides test assertion and wait for event functions to help test applications
built using Commanded.

The default assert and refute receive timeouts are one second.

You can override the default timeout in config (e.g. `config/test.exs`):

    config :commanded,
      assert_receive_event_timeout: 1_000,
      refute_receive_event_timeout: 1_000

# `assert_correlated`

Assert that events matching their respective predicates have a matching
correlation id.

Useful when there is a chain of events that is connected through event handlers.

## Example

    assert_correlated(
      BankApp,
      BankAccountOpened, fn opened -> opened.id == 1 end,
      InitialAmountDeposited, fn deposited -> deposited.id == 2 end
    )

# `assert_receive_event`

Assert that an event of the given event type is published.
Verify that event using the assertion function.

## Example

    assert_receive_event(BankApp, BankAccountOpened, fn opened ->
      assert opened.account_number == "ACC123"
    end)

# `assert_receive_event`

Assert that an event of the given event type, matching the predicate, is
published. Verify that event using the assertion function.

## Example

    assert_receive_event(BankApp, BankAccountOpened,
      fn opened -> opened.account_number == "ACC123" end,
      fn opened ->
        assert opened.balance == 1_000
      end)

# `refute_receive_event`

Refute that an event of the given type has been received.

An optional predicate may be provided to filter events matching the refuted
type.

## Examples

Refute that `ExampleEvent` is produced by given anonymous function:

    refute_receive_event(ExampleApp, ExampleEvent, fn ->
      :ok = MyApp.dispatch(command)
    end)

Refute that `ExampleEvent` is produced by `some_func/0` function:

    refute_receive_event(ExampleApp, ExampleEvent, &some_func/0)

Refute that `ExampleEvent` matching given `event_matches?/1` predicate function
is produced by `some_func/0` function:

    refute_receive_event(ExampleApp, ExampleEvent, &some_func/0,
      predicate: &event_matches?/1
    )

Refute that `ExampleEvent` matching given anonymous predicate function
is produced by `some_func/0` function:

    refute_receive_event(ExampleApp, ExampleEvent, &some_func/0,
      predicate: fn event -> event.value == 1 end
    )

Refute that `ExampleEvent` produced by `some_func/0` function is published to
a given stream:

    refute_receive_event(ExampleApp, ExampleEvent, &some_func/0,
      predicate: fn event -> event.value == 1 end,
      stream: "foo-1234"
    )

# `wait_for_event`

Wait for an event of the given event type to be published.

## Examples

    wait_for_event(BankApp, BankAccountOpened)

# `wait_for_event`

Wait for an event of the given event type, matching the predicate, to be
published.

## Examples

    wait_for_event(BankApp, BankAccountOpened, fn opened ->
      opened.account_number == "ACC123"
    end)

---

*Consult [api-reference.md](api-reference.md) for complete listing*
