Given when then with events
Event driven systems have a natural test shape. Given past events, when a command runs, then expect certain new events. Because state derives from events, you can set up any scenario by supplying its history rather than building objects by hand.
Testing each part
- Aggregates. Feed prior events, send a command, assert the emitted events. No database needed.
- Projections. Feed a sequence of events, assert the resulting read model.
- Sagas. Feed an event, assert the commands or events the saga reacts with.
Integration concerns
- Use a test broker or in memory bus to verify publishing and consuming.
- Assert idempotency by delivering the same event twice and checking the outcome is unchanged.
- Test out of order and duplicate delivery, since real brokers do both.
Why it is pleasant
Tests read like business rules, run fast because they avoid heavy infrastructure, and cover the exact behaviors that matter, namely which events result from which inputs.
Key idea
Event driven systems test cleanly with given past events, when a command, then expected events, plus checks for idempotency and out of order delivery.