O'Reilly logo

Implementing Domain-Driven Design by Vaughn Vernon

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Unit Testing and Specifications

Consider an added benefit of using Event Sourcing as we create unit tests. We can easily specify our tests in the form Given-When-Expect, as follows:

1. Given Events in the past

2. When Aggregate method is called

3. Expect the following Events or an exception

Here’s how it works. Past Events are used to set up the state of an Aggregate at the beginning of the unit test. We then execute the Aggregate method being tested, supplying test arguments and mock implementations of Domain Services as needed. Finally, we assert the expected results by comparing Events produced by an Aggregate with expected Events.

This approach allows us to capture and verify behaviors associated with each Aggregate. At the same time, we ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required