Class AggregateTestMutator
Reflection-based helpers for setting source-generated Maybe<T> backing fields in test scenarios. Works on the CLR object directly — no DbContext needed for the mutation itself.
public static class AggregateTestMutator
- Inheritance
-
AggregateTestMutator
- Inherited Members
Remarks
Use in integration tests to backdate time-dependent properties (e.g., SubmittedAt)
without raw SQL. Also usable in unit tests with FakeRepository<TAggregate, TId>.
These helpers are test-only. Do NOT use in production code.
Methods
ClearMaybeField<TEntity, TValue>(TEntity, Expression<Func<TEntity, Maybe<TValue>>>)
[RequiresUnreferencedCode("Uses reflection to set source-generated backing fields. Not AOT-compatible — test-only.")]
public static TEntity ClearMaybeField<TEntity, TValue>(this TEntity entity, Expression<Func<TEntity, Maybe<TValue>>> propertySelector) where TEntity : class where TValue : notnull
Parameters
entityTEntityThe entity to mutate.
propertySelectorExpression<Func<TEntity, Maybe<TValue>>>A lambda selecting the Maybe<T> property.
Returns
- TEntity
The same entity for fluent chaining.
Type Parameters
TEntityThe entity type.
TValueThe inner value type of the Maybe.
SetMaybeField<TEntity, TValue>(TEntity, Expression<Func<TEntity, Maybe<TValue>>>, TValue?)
Sets the source-generated backing field of a Maybe<T> property via reflection. Returns the entity for fluent chaining.
[RequiresUnreferencedCode("Uses reflection to set source-generated backing fields. Not AOT-compatible — test-only.")]
public static TEntity SetMaybeField<TEntity, TValue>(this TEntity entity, Expression<Func<TEntity, Maybe<TValue>>> propertySelector, TValue? value) where TEntity : class where TValue : notnull
Parameters
entityTEntityThe entity to mutate.
propertySelectorExpression<Func<TEntity, Maybe<TValue>>>A lambda selecting the Maybe<T> property (e.g.,
o => o.SubmittedAt).valueTValueThe value to set, or
nullto set to None.
Returns
- TEntity
The same entity for fluent chaining.
Type Parameters
TEntityThe entity type.
TValueThe inner value type of the Maybe.
Examples
order.SetMaybeField(o => o.SubmittedAt, DateTime.UtcNow.AddDays(-8))
.SetMaybeField(o => o.ShippedAt, DateTime.UtcNow.AddDays(-5));