Skip to main content
One of the most powerful benefits of Event Sourcing is how it transforms your unit testing capabilities. In a traditional CRUD system, writing unit tests for complex business rules is painful. You have to write extensive setup code, mock out databases or network clients, pre-populate schema rows, execute the action, and then query the database to assert that the state changed correctly. In an Event Sourced system, you have a unit-testing superpower:
  • establishing the pre-conditions of your system (the Given block) requires zero database setup or complex mocking. You simply provide an array of past historical events.
  • You then dispatch the command representing the action (the When block).
  • Finally, you assert that the expected new historical events were emitted, or that a specific domain validation error was raised (the Then block).

The Given-When-Then BDD Pipeline

Because Aggregate::apply and Aggregate::handle are pure, synchronous Rust functions without side effects, your unit tests are completely deterministic and execute in microseconds:

Complete Aggregate Test Suite

We provide a streamlined test helper called AggregateFixture to make writing BDD aggregate tests as simple as possible. Here is a complete test suite testing our Bank Account aggregate’s business rules using our actual testing helper:

Benefits of BDD Aggregate Testing

By utilizing AggregateFixture:
  1. No External Dependencies: You do not need to boot Docker database containers, mock HTTP servers, or configure test database schemas to assert that your business rules are correct.
  2. Instant Feedback Loop: Hundreds of business scenarios can execute in a fraction of a second, boosting developer confidence and enabling fast CI/CD pipelines.
  3. Living Documentation: The test cases read exactly like business specifications, matching the Ubiquitous Language defined with domain experts.