Skip to content

[Recipe] Process manager testing #20

Description

@slashdotdash

How to test a process manager produces the correct commands and state?

Use the process manager's handle/2 and apply/2 functions directly by reducing a list of one or more domain events. The resultant list of commands and process manager state can then be asserted on.

events = [%Event1{}, %Event2{}]
initial_state = %MyProcessManager{}

{commands, state} = 
  Enum.reduce(events, {[], initial_state}, fn event, {commands, state} ->
    new_commands = MyProcessManager.handle(state, event)
    new_state = MyProcessManager.apply(state, event)

    {commands ++ new_commands, new_state}
  end)

assert match?([%Command1{}, %Command2{}], commands)
assert match?(%MyProcessManager{key: value}, state)

This type of test can be run using async: true and will be very quick to execute as it does no IO.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions