Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Recipe] Process manager testing #20

Open
slashdotdash opened this issue Mar 19, 2021 · 0 comments
Open

[Recipe] Process manager testing #20

slashdotdash opened this issue Mar 19, 2021 · 0 comments

Comments

@slashdotdash
Copy link
Member

slashdotdash commented Mar 19, 2021

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant