- establishing the pre-conditions of your system (the
Givenblock) 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
Whenblock). - Finally, you assert that the expected new historical events were emitted, or that a specific domain validation error was raised (the
Thenblock).
The Given-When-Then BDD Pipeline
BecauseAggregate::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 calledAggregateFixture 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 utilizingAggregateFixture:
- 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.
- Instant Feedback Loop: Hundreds of business scenarios can execute in a fraction of a second, boosting developer confidence and enabling fast CI/CD pipelines.
- Living Documentation: The test cases read exactly like business specifications, matching the Ubiquitous Language defined with domain experts.