Lesson 71 of 93 · 6 min read · Updated
Value Objects, Entities and Aggregates: Three Building Blocks, Three Test Focuses
On this page
Domain-Driven Design gives three building blocks for a domain model: value objects, entities and aggregates. They look similar in code, since all of them are C# types with properties and methods, but they promise different things. A good test checks the promise. This lesson shows what each building block promises and how its tests differ, using a Money value object and an Order entity that is also an aggregate root.
Technical Requirements
- .NET 11 SDK
- xUnit 2.9.3
- FluentAssertions 7.2.2
Three Types, Three Focuses
| Type | Focus | Example test |
|---|---|---|
Value object (Money) | Structural equality, validation, immutability | TwoMoniesWithSameValues_AreEqual |
Entity (Order) | Identity, state transitions | TwoOrdersWithSameId_AreTheSameEntity_EvenWithDifferentContent |
Aggregate (Order) | Invariants inside the consistency boundary | AddLine_ExceedingMaxTotal_IsRejected_AndAggregateStaysConsistent |
The value object and the entity rows are covered in detail below. Aggregate invariants need more room and are covered separately; one aggregate rule appears at the end for contrast.
The Value Object: Money
A value object has no identity. Two instances with the same fields are the same value. It cannot be changed after it is created, and it cannot be created in an invalid state.
M08.LayeredDdd/Domain/ValueObjects/Money.cs
A readonly record struct gives structural equality and immutability for free, but the tests still check them. The type could be changed to a class later, and the tests are what keep the promise.
Testing a Value Object
The tests follow the three promises. First, equality by value, including the == operator and the hash code, so Money behaves correctly as a dictionary key:
M08.LayeredDdd.Tests/Domain/ValueObjectTests.cs
Second, validation. An invalid value must never exist, so the constructor itself is the thing under test:
M08.LayeredDdd.Tests/Domain/ValueObjectTests.cs
-0.01 is a boundary case just below zero. "T" and "TRYY" sit on both sides of the required three letters.
Third, immutability. An operation must return a new value and leave the original alone:
M08.LayeredDdd.Tests/Domain/ValueObjectTests.cs
The second argument to Be is the reason message shown on failure ("value objects must be immutable"). The same file also checks that adding different currencies throws and that amounts are rounded to two decimals.
The Entity: Order
An entity has an identity that stays the same while its content changes. Two Order objects with the same Id represent the same order, even if one has more lines. Order overrides equality to say so:
M08.LayeredDdd/Domain/Entities/Order.cs
Testing an Entity
The identity tests are the mirror image of the value object tests. Different content with the same identity means equal. Same content with different identities means not equal:
M08.LayeredDdd.Tests/Domain/EntityAndAggregateTests.cs
The second focus of entity tests is state transitions: draft to placed, placed to cancelled, and the transitions that are refused. Those tests are in the same file and follow the same pattern: arrange a known state, run one operation, check the new state.
A common mistake is to declare an entity as a record because records are short to write. A record compares all of its fields, so an order loaded before a line was added would no longer equal the same order after the change. Identity would silently become value equality. The first identity test above fails as soon as someone makes that change, which is exactly why it exists.
The Aggregate: A Consistency Boundary
The same Order class is also an aggregate root. As an aggregate it promises that its rules hold for everything inside its boundary, the order and its lines, after every operation. So an aggregate test checks two things: the invalid operation is refused, and the aggregate is unchanged afterwards.
M08.LayeredDdd.Tests/Domain/EntityAndAggregateTests.cs
Compare the three focuses: the value object test asks "are these two values equal?", the entity test asks "is this the same thing?", and the aggregate test asks "is the whole still valid after a refused change?".
Summary
- Value objects, entities and aggregates make different promises, so their tests focus on different things.
- Test a value object for structural equality (including
==and the hash code), validation in the constructor and immutability. - Test an entity for identity: same
Idmeans the same entity, whatever the content. - Test entity state transitions, both allowed and refused.
- Test an aggregate by checking that a refused operation leaves the whole aggregate unchanged.
- Do not model an entity as a record; the identity tests catch this mistake.