Class EventStoreTestHost
- Namespace
- Stratara.Testing.EntityFrameworkCore
- Assembly
- Stratara.Testing.EntityFrameworkCore.dll
A self-contained test host that runs the real Stratara event-sourcing write stack against a shared in-memory SQLite database. Construct it once per test, append events through the genuine IEventSource, and read aggregates back through the genuine IAggregationService — exercising production code paths without Postgres or Docker.
public sealed class EventStoreTestHost : IAsyncDisposable
- Inheritance
-
EventStoreTestHost
- Implements
- Inherited Members
- Extension Methods
Examples
await using var host = EventStoreTestHost.Create(s => s.AddAggregatesFromAssemblyContaining<Account>());
await host.ExecuteAsync(async events =>
{
await events.CreateAsync<Account>(id, new AccountOpened(id, tenantId, "Ada", 100m));
await events.SaveChangesAsync();
});
var account = await host.AggregateAsync<Account>(id);
Assert.Equal(100m, account!.Balance);
Remarks
Register the aggregates under test via the configure callback
(services.AddAggregatesFromAssemblyContaining<T>()) so event payload types
deserialize. Dispose the host (it is IAsyncDisposable) to close the SQLite
connection and tear down the database.
Fields
DefaultTenantId
The tenant id the host's default session context is scoped to.
public static readonly Guid DefaultTenantId
Field Value
Properties
Outbox
Records the event bundles emitted on each SaveChangesAsync, for assertions.
public RecordingEventBundleOutboxDispatcher Outbox { get; }
Property Value
Services
The root service provider, for resolving additional services in a scope.
public IServiceProvider Services { get; }
Property Value
Session
The preset session provider — reassign its context to switch the acting tenant/user.
public TestSessionContextProvider Session { get; }
Property Value
Methods
AggregateAsync<TAggregate>(Guid, CancellationToken)
Rehydrate an aggregate through the real IAggregationService in a fresh scope.
public Task<TAggregate?> AggregateAsync<TAggregate>(Guid streamId, CancellationToken cancellationToken = default) where TAggregate : notnull, new()
Parameters
streamIdGuidThe aggregate's stream id.
cancellationTokenCancellationTokenPropagated to the read.
Returns
Type Parameters
TAggregateThe aggregate type — must have a public parameterless constructor.
Create(Action<IServiceCollection>?)
Create a host backed by the built-in StrataraTestWriteDbContext.
public static EventStoreTestHost Create(Action<IServiceCollection>? configure = null)
Parameters
configureAction<IServiceCollection>Optional extra registration (e.g.
AddAggregatesFromAssemblyContaining<T>()).
Returns
- EventStoreTestHost
A ready-to-use host with the schema created.
Create<TWriteDbContext>(Action<IServiceCollection>?)
Create a host backed by a caller-supplied write DbContext type.
public static EventStoreTestHost Create<TWriteDbContext>(Action<IServiceCollection>? configure = null) where TWriteDbContext : DbContext, IWriteDbContext
Parameters
configureAction<IServiceCollection>Optional extra registration (e.g.
AddAggregatesFromAssemblyContaining<T>()).
Returns
- EventStoreTestHost
A ready-to-use host with the schema created.
Type Parameters
TWriteDbContextThe concrete write context (must subclass
WriteDbContext<T>).
DisposeAsync()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.
public ValueTask DisposeAsync()
Returns
- ValueTask
A task that represents the asynchronous dispose operation.
ExecuteAsync(Func<IEventSource, Task>)
Run work against the real IEventSource in a fresh scope.
public Task ExecuteAsync(Func<IEventSource, Task> work)
Parameters
workFunc<IEventSource, Task>The append/create +
SaveChangesAsyncwork to perform.