Class AggregateTestHarness<TAggregate>
A given/when/then harness that rehydrates an aggregate from a sequence of events using the same
reflection-based Apply dispatch as the production aggregation service — so a unit test
exercises the real apply logic without an event store, database, or session context.
public sealed class AggregateTestHarness<TAggregate> where TAggregate : class, new()
Type Parameters
TAggregateThe aggregate type — must have a public parameterless constructor.
- Inheritance
-
AggregateTestHarness<TAggregate>
- Inherited Members
- Extension Methods
Examples
var account = AggregateTestHarness<Account>
.Given(new AccountOpened(id, "Ada", 100m))
.And(new AmountWithdrawn(id, 30m))
.Build();
Assert.Equal(70m, account.Balance);
Remarks
Each event is dispatched to Apply(TEvent) if present, otherwise to
Apply(IEvent<TEvent>) (mirroring the framework's two-phase dispatch). Unlike
production — which silently ignores an event with no matching Apply overload — the harness
throws by default to surface a forgotten or mistyped overload; opt back into the lenient
behavior with IgnoringUnmappedEvents().
Methods
And(params object[])
Append more events to apply after the ones already staged.
public AggregateTestHarness<TAggregate> And(params object[] events)
Parameters
eventsobject[]The events to apply, in order.
Returns
- AggregateTestHarness<TAggregate>
The same harness for chaining.
Build()
Construct the aggregate and apply every staged event in order.
public TAggregate Build()
Returns
- TAggregate
The rehydrated aggregate.
Given(params object[])
Start a harness with the given prior events.
public static AggregateTestHarness<TAggregate> Given(params object[] events)
Parameters
eventsobject[]The events to apply, in order.
Returns
- AggregateTestHarness<TAggregate>
A harness seeded with
events.
GivenNoEvents()
Start a harness with no prior events (rehydrates a freshly constructed aggregate).
public static AggregateTestHarness<TAggregate> GivenNoEvents()
Returns
- AggregateTestHarness<TAggregate>
An empty harness.
IgnoringUnmappedEvents()
Match production's lenient dispatch: events with no matching Apply overload are skipped
instead of throwing.
public AggregateTestHarness<TAggregate> IgnoringUnmappedEvents()
Returns
- AggregateTestHarness<TAggregate>
The same harness for chaining.