# `Decree.Decision.Assertions`
[🔗](https://github.com/sparkle-space/decree/blob/v0.1.0/lib/decree/decision/assertions.ex#L1)

Test helpers for a `Decree.Decision`, for an application to import.

    use ExUnit.Case

    import Decree.Decision.Assertions

    test "a claimed email refuses a second claim" do
      assert_refuses(ClaimEmail, [%EmailClaimed{digest: "d"}], %ClaimEmailCommand{digest: "d"},
        :email_taken
      )
    end

    test "a free email is claimed" do
      assert_decides(ClaimEmail, [], command, %EmailClaimed{digest: "d"})
    end

They run `c:Decree.Decision.evolve/2`, `c:Decree.Decision.admit/2` and
`c:Decree.Decision.decide/2` with no store: the given events stand for what the reads
would answer, decrypted where the decision lists them, so they never read time or a
database. What only a store can answer, such
as a retry or a command match, needs a dispatch. So do a batch's scope and fact checks, which
run on prepared tags, and what a dispatch reduces: on a decision that lists its fold, a reason
here is the callback's whole. They check no tag index and no activation.

A failure is an `ExUnit.AssertionError`, which is why ExUnit has to be running, as it is
under `mix test`.

# `assert_decides`

```elixir
@spec assert_decides(module(), [struct()], struct(), term(), keyword()) :: term()
```

Asserts that the decision decides `expected`: an event, a list of events, or
`{:streams, members}`, as `c:Decree.Decision.decide/2` would answer it. Answers the state
the fold reached with the new events applied.

Takes the options of `decision_result/4`.

# `assert_query_items`

```elixir
@spec assert_query_items(module(), struct(), term(), keyword()) :: :ok
```

Asserts that `c:Decree.Decision.query/2` answers `expected` for the command.

## Options

  - `:metadata` - the dispatch metadata the tag context holds. Defaults to `%{}`.
  - `:business_events` - check `c:Decree.Decision.followup_query/3` instead, given these
    events.

# `assert_refuses`

```elixir
@spec assert_refuses(module(), [struct()], struct(), term(), keyword()) :: :ok
```

Asserts that the decision refuses with `reason`.

Takes the options of `decision_result/4`.

# `decision_result`

```elixir
@spec decision_result(module(), [struct()], struct(), keyword()) ::
  {:ok, [struct()]} | {:ok, term()} | {:error, term()}
```

Folds `given_events`, then admits and decides `command`, answering as a dispatch would.

  - `{:ok, events}`, every new event in append order, as a dispatch with
    `returning: :events` answers, or `{:ok, state}`, the fold with the new events applied,
    with `returning: :aggregate_state`.
  - `{:error, reason}`, the refusal a dispatch answers. `:duplicate` from
    `c:Decree.Decision.decide/2` is `{:error, :duplicate_command}`.

## Options

  - `:returning` - `:events`, the default, or `:aggregate_state`.
  - `:state` - the state to start from, instead of `c:Decree.Decision.initial_state/0`.

---

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