Lesson 73 of 93 · 6 min read · Updated
Testing Domain Events and Domain Services with Behavioral Tests
On this page
Domain events tell the rest of the system that something meaningful happened in the domain, such as an order being placed. Domain services hold business rules that do not belong to a single entity. Both are easy to test badly: events by checking only that "an event exists", services by testing them like infrastructure code. This lesson shows the three questions a domain event test must answer and the one question a domain service test must answer, with tests for an OrderPlaced event and a shipping discount service.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2
The Events and How the Aggregate Raises Them
An event reports a completed business fact, so its name is in the past tense. Its time is passed in from outside, which makes it testable.
M08.LayeredDdd/Domain/Events/DomainEvents.cs
Order collects events in a private list. Place adds OrderPlaced only after every check has passed and the status has changed. DequeueEvents returns the collected events and clears the list, so the application layer can publish them once:
M08.LayeredDdd/Domain/Entities/Order.cs
Three Questions for Every Event Test
| Question | Test |
|---|---|
| Was the event raised with meaningful data? | Place_RaisesOrderPlacedWithMeaningfulData |
| Was it raised too early? | AddLine_DoesNotRaiseAnyEvent, FailedPlace_DoesNotRaiseAnyEvent |
| Was it raised more than once? | Place_RaisesExactlyOneEvent_EvenAfterCancel, DequeueEvents_ClearsTheQueue |
Question 1: Meaningful Data
Checking that "some event exists" is not enough. A consumer of OrderPlaced needs the right order, the right customer, the right total and the right time. The test checks every field:
M08.LayeredDdd.Tests/Domain/DomainEventTests.cs
ContainSingle().Which.Should().BeOfType<OrderPlaced>().Subject does three things in one chain. It asserts there is exactly one event, asserts its type, and returns it typed as OrderPlaced for the field checks. OrderWorth1500() is a helper that creates a draft order with one line: two items at 750 each.
Question 2: Not Too Early
An event that is raised before the fact is complete is a lie. Downstream systems would react to an order that was never placed. Two tests guard against it:
M08.LayeredDdd.Tests/Domain/DomainEventTests.cs
The second test is the one that catches the common ordering bug: adding the event at the top of Place, before the checks. The exception would still be thrown, but the event would already be in the list.
Question 3: Not More Than Once
Duplicate events cause duplicate emails, double stock reservations and repeated payments. Two tests cover this from different sides:
M08.LayeredDdd.Tests/Domain/DomainEventTests.cs
The second call to DequeueEvents returning an empty list proves that a retry in the application layer cannot publish the same event twice. This matters because the handler passes the result of DequeueEvents to the publisher. If the queue kept its events, a second save-and-publish cycle on the same aggregate would send OrderPlaced again.
The Domain Service: A Business Result, Not Infrastructure Work
The shipping discount depends on the order total and on whether the customer is loyal. Loyalty is not part of the order, so the rule does not belong to Order. It lives in a domain service:
M08.LayeredDdd/Domain/Services/ShippingDiscountService.cs
The service receives the loyalty flag as a parameter. It does not ask a customer repository. That keeps it a pure function, and its tests become simple input and output checks:
M08.LayeredDdd.Tests/Domain/DomainEventTests.cs
The file also covers the cases above the threshold and below it for a regular customer. The boundary test pins down the >= operator, the same way the aggregate's limit test pins down its >.
A common mistake is to let a domain service call infrastructure, for example a repository or an email sender. Its tests then need mocks, and they start checking calls instead of business results. If a rule needs data from outside, let the application layer fetch it and pass it in.
Summary
- Test an event for meaningful data: check every field, including a time passed in from outside.
- Test that events are not raised too early, especially when an operation fails.
- Test that events are not raised twice, and that dequeuing clears them.
- A domain service holds rules that span entities and returns a business result.
- Keep domain services free of infrastructure so their tests stay simple input and output checks.