Skip to main content
Table of Contents

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

Guid

Properties

Outbox

Records the event bundles emitted on each SaveChangesAsync, for assertions.

public RecordingEventBundleOutboxDispatcher Outbox { get; }

Property Value

RecordingEventBundleOutboxDispatcher

Services

The root service provider, for resolving additional services in a scope.

public IServiceProvider Services { get; }

Property Value

IServiceProvider

Session

The preset session provider — reassign its context to switch the acting tenant/user.

public TestSessionContextProvider Session { get; }

Property Value

TestSessionContextProvider

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

streamId Guid

The aggregate's stream id.

cancellationToken CancellationToken

Propagated to the read.

Returns

Task<TAggregate>

The reconstructed aggregate, or null if the stream does not exist.

Type Parameters

TAggregate

The 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

configure Action<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

configure Action<IServiceCollection>

Optional extra registration (e.g. AddAggregatesFromAssemblyContaining<T>()).

Returns

EventStoreTestHost

A ready-to-use host with the schema created.

Type Parameters

TWriteDbContext

The 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

work Func<IEventSource, Task>

The append/create + SaveChangesAsync work to perform.

Returns

Task