Enact.Test (Enact v0.3.0)

Copy Markdown View Source

Shared test helpers for host-app action tests (and Enact's own suite).

import Enact.Test

test "rejects a blank name" do
  {:error, _} = result = Enact.run(CreateProject, %{"name" => ""}, actor: actor)
  assert_invalid(result, on: :name)
end

Intended for the test environment; the assertions require ExUnit.

Summary

Functions

Asserts the result is {:error, %Enact.Error{type: :invalid}}, and — given on: field — that the changeset carries an error on that top-level field. Returns the changeset for further assertions.

Builds an Enact.Context for unit-testing validate/2 callbacks or fetchers directly, without running the pipeline.

Flattens a changeset's errors into a map of field → messages, with nested embed errors as per-index lists of maps — the conventional errors_on helper, shipped so host apps don't reinvent it.

Functions

assert_invalid(result, opts \\ [])

@spec assert_invalid(
  term(),
  keyword()
) :: Ecto.Changeset.t()

Asserts the result is {:error, %Enact.Error{type: :invalid}}, and — given on: field — that the changeset carries an error on that top-level field. Returns the changeset for further assertions.

build_ctx(opts \\ [])

@spec build_ctx(keyword()) :: Enact.Context.t()

Builds an Enact.Context for unit-testing validate/2 callbacks or fetchers directly, without running the pipeline.

Options (all optional): :actor (default :test_actor), :subject, :params (default %{}), :repo, :mode (default :create), :assigns (default %{}).

:params are stored as given — this helper does not stringify keys. Enact.run/3 / dry_run/3 / subject/3 / authorized/3 do. Pass string keys here if the code under test pattern-matches on them.

errors_on(changeset)

@spec errors_on(Ecto.Changeset.t()) :: map()

Flattens a changeset's errors into a map of field → messages, with nested embed errors as per-index lists of maps — the conventional errors_on helper, shipped so host apps don't reinvent it.

assert %{milestones: [%{}, %{owner_id: ["not found"]}]} = errors_on(changeset)