Skip to content

ReadModelScenario: test Chronicle state from events

ReadModelScenario checks the state a Chronicle projection or reducer produces from a known event history, without a running server or database. Supply the events that explain the behavior you want to test, then assert on the resulting read model.

In .NET, ReadModelScenario<TReadModel> discovers the projection or reducer for the model and folds the collected events when you read a result. A fold applies the history to build state. In a Cratis Specification, Because() can collect that history; reading Instance in an assertion triggers the processing rather than merely retrieving an already-computed value.

The client tabs show each client’s workflow, not a promise of matching capabilities. The detailed harness behavior below is .NET-specific unless stated otherwise. If you are still choosing a projection or reducer, Choose how to build a read model compares the same history across those forms.

Use a scenario when a mapping or state transition is the question: after an order’s items change, does its summary contain the right total? A failure then points to the history and read-model logic without involving database setup or network delivery.

That test does not answer whether the sink stores the document correctly or an observer recovers after failure. Keep a kernel-backed integration test for those questions. What the harness stands in for separates the two boundaries.

ReadModelScenario<TReadModel> is in the Cratis.Chronicle.Testing NuGet package:

Terminal window
dotnet add package Cratis.Specifications.XUnit
dotnet add package Cratis.Chronicle.Testing
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
using Xunit;
[EventType]
public record TestingReadModelScenarioSomeEvent(string Value);
[EventType]
public record TestingReadModelScenarioSomeOtherEvent(int Value);
[FromEvent<TestingReadModelScenarioSomeEvent>]
public record TestingReadModelScenarioMyReadModel([Key] Guid Id, string Value);
public class when_projecting_events : Specification
{
readonly EventSourceId _eventSourceId = EventSourceId.New();
readonly ReadModelScenario<TestingReadModelScenarioMyReadModel> _scenario = new();
Task Because() =>
_scenario.Given
.ForEventSource(_eventSourceId)
.Events(new TestingReadModelScenarioSomeEvent("expected value"), new TestingReadModelScenarioSomeOtherEvent(42));
[Fact] void should_project_the_value() =>
_scenario.Instance!.Value.ShouldEqual("expected value");
}

View C# snippet source on GitHub

The .NET example supplies a value-setting event and an unrelated event. Reading Instance processes them and checks the mapped value; the unrelated event does not change it. Use WithStrictEventSubscription() if you want the scenario to reject that unrelated projection input instead.

In TypeScript, collect events with given.forEventSource(id).events(...) and await instance. Kotlin folds events through scenario.fold(...). The Java example calls the reducer directly and asserts on its return value, so it does not check scenario dispatch.

Fold appended events with their original context (.NET)

Section titled “Fold appended events with their original context (.NET)”

When rebuilding a read model from events that have already been appended, pass the client’s IEnumerable<AppendedEvent> to await scenario.ProcessAppendedEvents(events). Unlike bare events passed through ProcessEventsFor or Given, these events retain their original sequence numbers, occurrence times (including offsets), source and stream metadata, correlation, and causation.

Input accumulates until you access Instance, Instances, or InstanceForEventSourceId. Every rebuild sorts the collected events by sequence number. Use distinct sequence numbers; duplicate slots fail when preparing the input. Bare events mixed into the same scenario receive synthetic contexts with collection-index sequence numbers. Projections retain their normal key resolution, including event-property keys, joins, and children. Reducers fold each event source independently; use Instances or InstanceForEventSourceId if more than one source materializes. The input is not appended to the scenario’s application event log.

In .NET, use Given.ForEventSource(id).OnStream(eventSourceType, eventStreamType, eventStreamId).Events(...) when your read model needs the event’s source or stream route. The route reaches the projection or reducer through EventContext.EventSourceType, EventContext.EventStreamType, and EventContext.EventStreamId.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
using Xunit;
[EventType]
public record TestingReadModelScenarioPaymentReceived(decimal Amount);
[FromEvent<TestingReadModelScenarioPaymentReceived>]
public record TestingReadModelScenarioPaymentRoute(
[Key] Guid Id,
decimal Amount,
[SetFromContext<TestingReadModelScenarioPaymentReceived>(nameof(EventContext.EventSourceType))] EventSourceType SourceType,
[SetFromContext<TestingReadModelScenarioPaymentReceived>(nameof(EventContext.EventStreamType))] EventStreamType StreamType,
[SetFromContext<TestingReadModelScenarioPaymentReceived>(nameof(EventContext.EventStreamId))] EventStreamId StreamId);
public class when_seeding_a_routed_payment : Specification
{
readonly EventSourceId _orderId = EventSourceId.New();
readonly ReadModelScenario<TestingReadModelScenarioPaymentRoute> _scenario = new();
Task Because() =>
_scenario.Given
.ForEventSource(_orderId)
.OnStream(eventSourceType: "Order", eventStreamType: "Payments", eventStreamId: "2026-10")
.Events(new TestingReadModelScenarioPaymentReceived(42m));
[Fact] void should_see_the_source_type() => _scenario.Instance!.SourceType.ShouldEqual(new EventSourceType("Order"));
[Fact] void should_see_the_stream_type() => _scenario.Instance!.StreamType.ShouldEqual(new EventStreamType("Payments"));
[Fact] void should_see_the_stream_id() => _scenario.Instance!.StreamId.Value.ShouldEqual("2026-10");
}

View C# snippet source on GitHub

All three OnStream arguments are optional. Omitting an argument, or passing null, uses the same default as appending to the event log:

Argument Default
eventSourceType EventSourceType.Default
eventStreamType EventStreamType.All
eventStreamId EventStreamId.Default

For example, OnStream(eventStreamType: "Payments") seeds a payment stream with the default source type and stream id. Leaving out OnStream keeps these defaults too. The method returns a new builder, so reusing the original builder does not inherit the route. Calling ReadModel(instance) still registers the instance by its event source id, regardless of the builder’s route.

Reducer source and stream filters apply to seeded events just as they do under the kernel. A reducer carrying [EventSourceType("Order")] ignores events from other source types; [EventStreamType("Payments")] narrows by stream type. [FromEventSource<TSource>("Payments")] resolves both filters from the registered event source definition. Both filters must match, and neither filters by stream id. An unspecified source filter (EventSourceType.Unspecified) and EventStreamType.All do not narrow delivery. An explicit source filter of EventSourceType.Default matches only that source type.

Projections have no source-type or stream-type filters: they consume the event types their definitions subscribe to, across every route. Use context mappings such as [SetFromContext<TEvent>] to inspect the route in a projected property.

WithTags(params string[] tags) adds append tags to seeded events. It returns a new builder, and preserves OnStream metadata in either call order. The effective tags combine the event’s static/inherited Tag/Tags with the builder’s append tags; named tags remain separate and do not satisfy FilterEventsByTag.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Testing;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
[Tag("origin")]
public record ScenarioBalanceAdded(decimal Amount);
[FilterEventsByTag("accepted")]
[EventSourceType("Account")]
[EventStreamType("Payments")]
public record ScenarioSelfBalance(decimal Balance)
{
public static ScenarioSelfBalance Reduce(ScenarioBalanceAdded @event, ScenarioSelfBalance? current) =>
new((current?.Balance ?? 0m) + @event.Amount);
}
public static class SelfReducerTagScenario
{
public static async Task Run()
{
var artifacts = new InMemoryClientArtifactsProvider
{
EventTypes = [typeof(ScenarioBalanceAdded)],
Reducers = [typeof(ScenarioSelfBalance)]
};
var scenario = new ReadModelScenario<ScenarioSelfBalance>(null, new Defaults(artifacts));
var source = scenario.Given.ForEventSource("account-1");
await source.WithTags("accepted").OnStream("Account", "Payments")
.Events(new ScenarioBalanceAdded(3m), new ScenarioBalanceAdded(5m));
await source.OnStream("Account", "Payments").WithTags("rejected")
.Events(new ScenarioBalanceAdded(100m));
scenario.Instance!.Balance.ShouldEqual(8m);
}
}

View C# snippet source on GitHub

Classic and self reducers use the same registry-selected invoker and metadata matcher. Filter tags are OR alternatives, while tag/source/stream categories are AND requirements. Descriptive tags on the reducer do not become filters. This adds reducer tag fidelity without changing projection or reactor scenario selection; compliance, replay and real-store behavior still need kernel-backed specifications.

In the .NET scenario, seeded events for projection-backed read models pass through the IEventSerializer supplied by Defaults, then through Chronicle’s schema-aware JSON conversion. Projections therefore consume the event’s serialized representation, including serializer-owned property names, rather than a reflection walk of the CLR object. This also avoids reflecting over the parent links inside a nonempty JsonObject event property. Serialized content is re-read as case-sensitive JSON data before conversion, so a serializer’s case-insensitive CLR member matching cannot collapse distinct dictionary keys. This preserves JSON data spelling; it is not a verification of the network marshaller or wire compatibility.

This fidelity applies to event input for fluent and model-bound projections. Reducers still receive typed events directly. The optional initial read model state goes through the same schema-aware conversion: it is serialized, re-read as case-sensitive JSON data, and converted against the read model’s schema, so an initial state may contain nonempty JsonObject values. Reducers receive the initial state as the typed instance.

In .NET, pass an initial state to the constructor when the read model starts from a non-default baseline:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
public record TestingReadModelScenarioItemAdded;
public record TestingReadModelScenarioCountingReadModel(int Count);
public class TestingReadModelScenarioCountingReducer : IReducerFor<TestingReadModelScenarioCountingReadModel>
{
public TestingReadModelScenarioCountingReadModel ItemAdded(TestingReadModelScenarioItemAdded @event, TestingReadModelScenarioCountingReadModel? current, EventContext context) =>
current is null ? new(1) : current with { Count = current.Count + 1 };
}
public static class TestingReadModelScenarioInitialState
{
public static async Task Run()
{
var myId = EventSourceId.New();
var initial = new TestingReadModelScenarioCountingReadModel(10);
var scenario = new ReadModelScenario<TestingReadModelScenarioCountingReadModel>(initial);
await scenario.Given
.ForEventSource(myId)
.Events(new TestingReadModelScenarioItemAdded());
scenario.Instance!.Count.ShouldEqual(11);
}
}

View C# snippet source on GitHub

In .NET, pass an IServiceProvider to the constructor to supply mocks and stubs for an instance reducer’s fold or a fluent projection’s definition construction. Static self-reduction methods do not accept service arguments:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
[EventType]
public record TestingReadModelScenarioOrderCreated(string OrderId);
public record TestingReadModelScenarioOrderSummary(string OrderId, decimal Total);
public interface ITestingReadModelScenarioPricingService
{
decimal GetBasePrice();
}
public class TestingReadModelScenarioOrderSummaryReducer(ITestingReadModelScenarioPricingService pricingService) : IReducerFor<TestingReadModelScenarioOrderSummary>
{
public TestingReadModelScenarioOrderSummary OrderCreated(TestingReadModelScenarioOrderCreated @event, TestingReadModelScenarioOrderSummary? current, EventContext context) =>
new(@event.OrderId, pricingService.GetBasePrice());
}
public static class TestingReadModelScenarioInjectingDependencies
{
public static async Task Run()
{
var pricingService = Substitute.For<ITestingReadModelScenarioPricingService>();
var services = new ServiceCollection()
.AddSingleton(pricingService)
.BuildServiceProvider();
var scenario = new ReadModelScenario<TestingReadModelScenarioOrderSummary>(initialState: null, serviceProvider: services);
await scenario.Given
.ForEventSource("order-1")
.Events(new TestingReadModelScenarioOrderCreated("order-1"));
scenario.Instance!.Total.ShouldEqual(0m);
}
}

View C# snippet source on GitHub

In .NET, use Given.ForEventSourceId(id).ReadModel(instance) when one key already has state before your events. The projection starts from that instance exactly as supplied, including its identifier: setters, arithmetic and children apply on top of the seeded values. A reducer receives the seed as its current state for that key’s first event. Other keys start from the constructor’s optional initial state or the normal empty baseline.

A seeded key appears in Instances, InstanceForEventSourceId, and (when it is the only instance) Instance, even without events. The supplied id addresses the read-model key, which can differ from an event source when you use UsingKey or joins. Seeding the same key twice throws ReadModelAlreadySeeded.

You can also pass scenario.ReadModels to production code that calls IReadModels.GetInstanceById:

using Cratis.Chronicle.ReadModels;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
public class TestingReadModelScenarioOrderService(IReadModels readModels)
{
public async Task<decimal> GetOrderTotal(string orderId)
{
var order = await readModels.GetInstanceById<TestingReadModelScenarioOrderSummary>(orderId);
return order.Total;
}
}
public static class TestingReadModelScenarioPreseedingInstances
{
public static async Task Run()
{
var scenario = new ReadModelScenario<TestingReadModelScenarioOrderSummary>();
await scenario.Given
.ForEventSourceId("order-1")
.ReadModel(new TestingReadModelScenarioOrderSummary("order-1", 99.99m));
// This key exists even without events. Any later events fold on top of this seed.
scenario.InstanceForEventSourceId("order-1")!.Total.ShouldEqual(99.99m);
// Pass scenario.ReadModels to production code under test
var sut = new TestingReadModelScenarioOrderService(scenario.ReadModels);
var result = await sut.GetOrderTotal("order-1");
result.ShouldEqual(99.99m);
}
}

View C# snippet source on GitHub

scenario.ReadModels returns an IReadModels instance that intercepts GetInstanceById for registered instances, with collected events applied before each lookup. Models without a projection or reducer remain lookup-only seeds and do not appear in Instances. The rest of the surface delegates to the production implementation and needs a running Chronicle: enumerate scenario output through Instances, not GetInstances.

In .NET, use Given.ForKey(parts).ReadModel(instance) when the selected projection uses a root UsingCompositeKey. Supply exactly its mapped target names, as serialized by the projection naming policy; do not assume CLR spelling, invent aliases, or supply an encoded key string. For example, the current policy maps TenantId to tenantId but preserves URLId. Unmapped identifier properties are not key parts.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
public static class TestingReadModelScenarioCompositeSeeding
{
public static async Task Run()
{
using var scenario = new ReadModelScenario<SeededCompositeOrder>().WithProjection(builder => builder
.From<CompositeOrderUpdated>(from => from
.UsingCompositeKey<SeededCompositeOrderKey>(key => key
.Set(part => part.TenantId).To(@event => @event.TenantId)
.Set(part => part.OrderNumber).To(@event => @event.OrderNumber))
.Increment(model => model.Revision)));
var parts = new Dictionary<string, object?>
{
["tenantId"] = "tenant-a",
["orderNumber"] = 42
};
await scenario.Given.ForKey(parts).ReadModel(new(new("tenant-a", 42), 5));
await scenario.Given.ForEventSourceId("source").Events(new CompositeOrderUpdated("tenant-a", 42));
scenario.InstanceForKey(parts)!.Revision.ShouldEqual(6);
}
}
[EventType]
public record CompositeOrderUpdated(string TenantId, int OrderNumber);
public record SeededCompositeOrderKey(string TenantId, int OrderNumber);
public record SeededCompositeOrder(SeededCompositeOrderKey Id, int Revision);

View C# snippet source on GitHub

The seed participates in the same native sink identity as event-built instances. Events for that key update the seed; other keys start normally. Parts are detached when supplied, so later dictionary changes do not retarget the seed. Equivalent normalized keys, including reordered parts, throw ReadModelAlreadySeeded without replacing the original seed. CollectReadModelFor(parts, instance) registers the same starting state synchronously.

Use InstanceForKey(parts) for a registered named seed’s current state, including before any events. It returns null for unregistered keys or a removed instance; it is not a general composite-key query for event-built instances. It throws InvalidReadModelSeedKey for missing, extra or invalid key parts rather than returning null. Instances still exposes the native encoded identities. The seed’s identifier is preserved as supplied, just as with scalar seeds.

Part values must be non-null scalars compatible with the identifier schema, including supported concepts and defined enum values. Missing, extra, differently cased or invalid parts throw InvalidReadModelSeedKey before registration. Nested key parts, missing or ambiguous schema paths, incompatible root mappings, scalar-key projections, child-only composite mappings and reducers are refused rather than guessed.

A scalar seed (Given.ReadModel or CollectReadModelFor(eventSourceId, ...)) and a named seed that resolve to the same storage identity are refused with ConflictingReadModelSeeds instead of letting the last one win.

Select WithProjection before registering named seeds if you need to override discovery. Once a named seed is registered, changing the projection throws ReadModelSeedProjectionCannotChange, even after processing. There is no reset operation. Existing scalar-key and reducer seeding APIs are unchanged.

A seeded parent without its originating events cannot resolve children whose parent key comes from those events. Include the originating events for those shapes. Children whose parent key resolves directly, without a parent-event lookup, can append onto the seeded parent.

The testing event store supports collection observations through ReadModels.Observe<T>(). It rebuilds the complete collection from its scenario event log on subscription and after successful appends, including batch appends and unit-of-work commits. Projections process events in log order; reducers fold each event source separately. Explicitly seeded instances are included, and the optional filter is applied to the rebuilt set. Unchanged sets do not emit again.

Own and dispose the subscription when the test no longer needs it. After the last subscriber leaves, or the subject completes or is disposed, further appends produce no emissions for that observation. The harness uses the projection and reducer processors described below; it does not verify kernel transport, sink notifications, or read-model compliance protection.

ReadModelsForTesting(inner, eventLog) also provides this behavior when you have an existing scenario log. When inner comes from the testing event store, its artifact catalog, explicit projections, and injected services are retained; otherwise default artifact discovery is used. The constructor without an event log refuses observation with ReadModelCannotBeObserved. Passive read models and event-target types are refused just as they are by the client.

ReadModelScenario<T>.ReadModels.Observe<T>() remains unsupported: that scenario collects its own input rather than appending it to the event log. Use Instance, Instances, or InstanceForEventSourceId to assert on that scenario’s output.

When a test generates artifacts for one run, give that scenario its own artifact catalog. The next scenario can then select a different projection without inheriting the first run’s artifacts:

var scenario = new ReadModelScenario<MyReadModel>(null, new Defaults(artifacts))
.WithProjection(builder => builder.From<MyEvent>(from =>
from.Set(model => model.Name).To(e => e.Name)));
await scenario.Given.ForEventSource(id).Events(new MyEvent("example"));
var result = scenario.Instance;

View C# snippet source on GitHub

In .NET, supply Defaults built from an IClientArtifactsProvider to control event types, serializers, and discovery during seeding. WithProjection can define a projection for one scenario and takes precedence over discovered projections and reducers; it does not register the projection process-wide. TypeScript selects the projection from the catalog passed to the scenario instead.

Use InMemoryClientArtifactsProvider when you want to supply artifact lists without assembly discovery. Its settable collections default to empty; include every event type your scenario uses in EventTypes. You can also supply projection, model-bound projection, reducer, reactor, middleware, constraint, migration, seeding, event-source, compliance, and security provider types through their corresponding collections.

Pass ChronicleOptions as the third argument to new Defaults(artifacts, serviceProvider, options); use null for the service provider when you do not need dependency injection. Configure options.ExplicitArtifacts.RegisterProjection<TReadModel>(...), RegisterReadModel<TReadModel>(), or RegisterReducer<TReadModel>() for a self model before constructing the defaults. Explicit projections use the same discovery recipes as the client and do not need a projection class or an entry in the provider’s projection collections.

ReadModelScenario<TReadModel>(null, defaults) honors the explicitly registered builder. WithProjection still takes precedence over it. Pass the same defaults to EventScenario or ReactorScenario<TReactor> to retain the run’s artifact catalog and settings. The test event store applies registrations in-process; appending through an event scenario does not start observer delivery or automatically run those projections and reactors.

Fold read models through an existing store (.NET)

Section titled “Fold read models through an existing store (.NET)”

When test infrastructure folds several read models against one EventStoreForTesting, borrow that store rather than creating one store per scenario. For example, a command test harness may need to build each read model the command reads. Ask the existing store for a scenario:

using var scenario = store.CreateReadModelScenario<OrderSummary>();
await scenario.ProcessAppendedEvents(selectedEvents);
var summary = scenario.Instance;

View C# snippet source on GitHub

The scenario borrows the store’s discovered artifacts, event types, serialization and event type schemas, and owns everything else: its input, seeds, Instance/Instances, WithProjection and its ReadModels surface. It folds only the events you give it, never the store’s event log, so pass the events you selected from the log to ProcessAppendedEvents. Two scenarios from the same store never see each other’s input or seeds, and the store’s own ReadModels is unaffected.

Pass an IServiceProvider to activate reducers and projections from it; otherwise the store’s provider is used, or one built from the scenario’s Services. Serialization and compliance always come from the store. Disposing the scenario releases only what it created; the store and its service provider stay usable.

In .NET, ReadModelScenario<TReadModel> looks up a handler in the client artifacts registry. By default, Chronicle’s reflection-based discovery resolves that registry once per process from project-referenced and package-referenced assemblies. Supplying Defaults uses its IClientArtifactsProvider instead, so each run can discover a different set of handlers. Without an inline projection, handlers are selected in this order:

  1. Reducer — a classic IReducerFor<TReadModel>, explicitly nominated plain instance reducer, or structurally recognized self-reducing model.
  2. Fluent projection — a class implementing IProjectionFor<TReadModel>.
  3. Model-bound projection — TReadModel carries genuine projection mappings such as [FromEvent<T>]; a key or shared observer configuration alone does not establish mapping ownership.

WithProjection takes precedence over all three, including a discovered reducer. If none are found and no inline projection was supplied, NoReadModelHandlerFound is thrown. The scenario has no overload for authoring a projection directly in Projection Declaration Language (PDL).

When a spec generates artifacts at runtime or needs a different mapping for the same read model, pass new Defaults(artifactsProvider) to new ReadModelScenario<MyReadModel>(null, defaults) and call WithProjection(builder => builder.From<MyEvent>(from => from.Set(model => model.Name).To(e => e.Name))). The callback uses IProjectionBuilderFor<MyReadModel> and replaces discovered projection selection for that scenario only. Each scenario can have its own Defaults, artifact registry, and fluent definition; no wire contract or process-wide projection registration is needed. The scenario’s event store also uses that registry.

In .NET, that registry is not private to the scenario. scenario.ClientArtifactsProvider hands out the very IClientArtifactsProvider the scenario resolved its handler from, so a spec can ask what Chronicle registered instead of re-deriving it by reflecting over its own assemblies:

using Cratis.Chronicle.Testing;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
public static class TestingReadModelScenarioRegisteredArtifacts
{
public static void Run()
{
var scenario = new ReadModelScenario<TestingReadModelScenarioModelBoundDeliveryStatus>();
// The registry Chronicle discovered its artifacts from - no reflection of your own needed
var artifacts = scenario.ClientArtifactsProvider;
artifacts.ModelBoundProjections.ShouldContain(typeof(TestingReadModelScenarioModelBoundDeliveryStatus));
artifacts.EventTypes.ShouldContain(typeof(TestingReadModelScenarioModelBoundShipmentDispatched));
// The same registry, reachable without a scenario
Defaults.Instance.ClientArtifactsProvider.Reactors.ShouldNotBeNull();
}
}

View C# snippet source on GitHub

It exposes every artifact kind Chronicle discovers — EventTypes, Projections, ModelBoundProjections, Reactors, ReadModelReactors, Reducers, ReactorMiddlewares, ConstraintTypes, UniqueConstraints, UniqueEventTypeConstraints, RemoveConstraintEventTypes, EventTypeMigrators, EventSeeders, and the compliance and additional-event-information providers — including the classifications the registry draws itself. An event type with a property-level [Unique] lands in UniqueConstraints, while one with a class-level [Unique] lands in UniqueEventTypeConstraints; asking the registry gets that distinction right without reimplementing it.

The property is read-only: reading it neither triggers nor alters registration, and every read hands out the same instance.

The .NET projection scenario ignores event types the projection does not subscribe to. That lets a test use a history containing audit or marker events without making them part of the projection. If every supplied event is meant to contribute, call WithStrictEventSubscription(). An unrelated event then raises UnsubscribedEventSeeded, naming its type:

var scenario = new ReadModelScenario<OrderSummary>().WithStrictEventSubscription();

View C# snippet source on GitHub

This applies to projection-backed read models. A reducer only invokes the handlers it declares, so an unsubscribed seeded event is inherently a no-op there, exactly as at runtime.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Reducers;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
public record TestingReadModelScenarioReducerOrderCreated(string OrderId);
[EventType]
public record TestingReadModelScenarioReducerItemAdded(decimal Price);
public record TestingReadModelScenarioReducerOrderSummary(string OrderId, decimal Total);
public class TestingReadModelScenarioReducerOrderSummaryReducer : IReducerFor<TestingReadModelScenarioReducerOrderSummary>
{
public TestingReadModelScenarioReducerOrderSummary OnOrderCreated(TestingReadModelScenarioReducerOrderCreated @event, TestingReadModelScenarioReducerOrderSummary? current, EventContext context) =>
new(@event.OrderId, 0m);
public TestingReadModelScenarioReducerOrderSummary OnItemAdded(TestingReadModelScenarioReducerItemAdded @event, TestingReadModelScenarioReducerOrderSummary? current, EventContext context) =>
current! with { Total = current.Total + @event.Price };
}
public static class TestingReadModelScenarioReducerExample
{
public static async Task Run()
{
var orderId = "order-1";
var scenario = new ReadModelScenario<TestingReadModelScenarioReducerOrderSummary>();
await scenario.Given
.ForEventSource(orderId)
.Events(
new TestingReadModelScenarioReducerOrderCreated("order-1"),
new TestingReadModelScenarioReducerItemAdded(9.99m),
new TestingReadModelScenarioReducerItemAdded(4.50m));
scenario.Instance!.Total.ShouldEqual(14.49m);
}
}

View C# snippet source on GitHub

The .NET example checks an order total after two items are added. Each reducer handler receives the current state and returns the next state; the final assertion checks the combined result of those transitions.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Projections;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
public record TestingReadModelScenarioFluentProductCreated(string Name);
[EventType]
public record TestingReadModelScenarioFluentStockAdjusted(int NewStock);
public record TestingReadModelScenarioFluentProductView(string Name, int Stock);
public class TestingReadModelScenarioFluentProductViewProjection : IProjectionFor<TestingReadModelScenarioFluentProductView>
{
public void Define(IProjectionBuilderFor<TestingReadModelScenarioFluentProductView> builder) =>
builder
.From<TestingReadModelScenarioFluentProductCreated>()
.From<TestingReadModelScenarioFluentStockAdjusted>(_ => _
.Set(m => m.Stock).To(e => e.NewStock));
}
public static class TestingReadModelScenarioFluentProjectionExample
{
public static async Task Run()
{
var productId = "product-1";
var scenario = new ReadModelScenario<TestingReadModelScenarioFluentProductView>();
await scenario.Given
.ForEventSource(productId)
.Events(
new TestingReadModelScenarioFluentProductCreated("Widget"),
new TestingReadModelScenarioFluentStockAdjusted(100));
scenario.Instance!.Name.ShouldEqual("Widget");
scenario.Instance!.Stock.ShouldEqual(100);
}
}

View C# snippet source on GitHub

The .NET projection maps the product name by convention and explicitly maps NewStock to Stock. Both assertions matter: they check that the conventional mapping and the differently named property mapping contribute to the same result.

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
public record TestingReadModelScenarioModelBoundShipmentDispatched(string Carrier);
[EventType]
public record TestingReadModelScenarioModelBoundShipmentDelivered(DateTimeOffset DeliveredAt);
[FromEvent<TestingReadModelScenarioModelBoundShipmentDispatched>]
[FromEvent<TestingReadModelScenarioModelBoundShipmentDelivered>]
public record TestingReadModelScenarioModelBoundDeliveryStatus(
[Key] string ShipmentId,
string Carrier,
DateTimeOffset? DeliveredAt);
public static class TestingReadModelScenarioModelBoundExample
{
public static async Task Run()
{
var shipmentId = "shipment-1";
var scenario = new ReadModelScenario<TestingReadModelScenarioModelBoundDeliveryStatus>();
await scenario.Given
.ForEventSource(shipmentId)
.Events(
new TestingReadModelScenarioModelBoundShipmentDispatched("FedEx"),
new TestingReadModelScenarioModelBoundShipmentDelivered(DateTimeOffset.UtcNow));
scenario.Instance!.Carrier.ShouldEqual("FedEx");
scenario.Instance!.DeliveredAt.HasValue.ShouldBeTrue();
}
}

View C# snippet source on GitHub

Here the mapping lives on the read-model type rather than in a separate projection class. The test still supplies event history and checks the resulting state.

In .NET, assert [ChildrenFrom<TEvent>] child rows directly on the resulting model. The scenario accepts these child-key types:

  • string, int/long, bool, enum, Guid
  • the temporal primitives DateOnly, TimeOnly, DateTime, DateTimeOffset
  • a ConceptAs<T> over any of the above

Parent and child events can share an event source, or children can arrive on another source linked by parentKey. Events carrying the same child key merge into one child. Cross-stream parent-key handling is simulated in-process; check Substitutions before treating a passing child-row assertion as coverage of server delivery.

using System.Linq;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
public record TestingReadModelScenarioChildrenTimesheetStarted(int Year);
[EventType]
public record TestingReadModelScenarioChildrenDayRecorded(DateOnly Day, decimal Hours);
public record TestingReadModelScenarioChildrenTimesheetDay(DateOnly Day, decimal Hours);
[FromEvent<TestingReadModelScenarioChildrenTimesheetStarted>]
public record TestingReadModelScenarioChildrenTimesheet(
[Key] Guid Id,
int Year,
[ChildrenFrom<TestingReadModelScenarioChildrenDayRecorded>(key: nameof(TestingReadModelScenarioChildrenDayRecorded.Day))]
IEnumerable<TestingReadModelScenarioChildrenTimesheetDay> Days);
public static class TestingReadModelScenarioChildren
{
public static async Task Run()
{
var scenario = new ReadModelScenario<TestingReadModelScenarioChildrenTimesheet>();
await scenario.Given
.ForEventSource(Guid.NewGuid())
.Events(
new TestingReadModelScenarioChildrenTimesheetStarted(2026),
new TestingReadModelScenarioChildrenDayRecorded(new DateOnly(2026, 6, 1), 7.5m),
new TestingReadModelScenarioChildrenDayRecorded(new DateOnly(2026, 6, 2), 8m));
scenario.Instance!.Days.Count().ShouldEqual(2);
scenario.Instance!.Days.First().Day.ShouldEqual(new DateOnly(2026, 6, 1));
scenario.Instance!.Days.First().Hours.ShouldEqual(7.5m);
}
}

View C# snippet source on GitHub

The .NET scenario supports model-bound ([VariantOf<T>]) and fluent (.VariantOf<T>(...)) variants. It discovers the other variants of the group from the registered artifacts, so entering one variant removes the entity from the others, and a non-entering event that was seeded before the entering event is on the variant when it is created. Use one scenario per variant, seeded with the same events, to specify a move from one variant to the next.

Multiple instances (joins and cross-stream)

Section titled “Multiple instances (joins and cross-stream)”

A join can produce several keyed documents, including one for the joined source. If the .NET scenario chose one arbitrarily, an assertion could inspect the wrong entity. Use InstanceForEventSourceId to select the intended result even when another source’s event was seeded first:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
[EventType]
public record TestingReadModelScenarioJoinCustomerRegistered(string Name);
[EventType]
public record TestingReadModelScenarioJoinOrderPlaced(string CustomerId, decimal Amount);
[FromEvent<TestingReadModelScenarioJoinOrderPlaced>]
public record TestingReadModelScenarioJoinOrder(
[Key] string Id,
string CustomerId,
[Join<TestingReadModelScenarioJoinCustomerRegistered>(on: nameof(CustomerId), eventPropertyName: nameof(TestingReadModelScenarioJoinCustomerRegistered.Name))] string CustomerName,
decimal Amount);
public static class TestingReadModelScenarioMultipleInstances
{
public static async Task Run()
{
var scenario = new ReadModelScenario<TestingReadModelScenarioJoinOrder>();
var customerId = EventSourceId.New();
var orderId = EventSourceId.New();
await scenario.Given.ForEventSource(customerId).Events(new TestingReadModelScenarioJoinCustomerRegistered("Ada"));
await scenario.Given.ForEventSource(orderId).Events(new TestingReadModelScenarioJoinOrderPlaced(customerId.Value, 100m));
var order = scenario.InstanceForEventSourceId(orderId);
order!.CustomerName.ShouldEqual("Ada");
}
}

View C# snippet source on GitHub

Instances exposes the materialized documents by resolved root key. Projections and reducers fed through ProcessAppendedEvents(...) populate keyed results. Reducers fed only bare events keep a single fold across sources unless per-key read models were seeded.

If more than one keyed instance materializes, reading Instance throws MultipleInstancesMaterialized. Select the intended instance rather than weakening the assertion to accept whichever model happens to appear first.

The .NET projection scenario runs the production projection engine. Its event input passes through the configured IEventSerializer and schema-aware JSON conversion, including persisted input used for keys and joins. Reducer-backed scenarios use the client reducer invoker instead.

A passing assertion checks the result under those in-process conditions. It does not prove that a deployed Chronicle stores or reads it correctly. Use this table to decide which other behavior your test needs to cover:

Layer What runs instead
The read model sink Projections write to an in-memory sink and round-trip documents through JSON, not a database’s encoding. Reducers return their folded state without a real read-model sink.
Storage and the observer lifecycle Chronicle’s production in-memory storage holds seeded events, including their original and migrated generations and content hashes. No observers run: there is no partition scheduling, replay or rewind.
The event context Bare events receive synthetic contexts with collection-position sequence numbers. ProcessAppendedEvents(...) preserves original contexts and sorts the accumulated input by sequence number. Neither input path tests server delivery.
The document key mapping The _id ↔ identifier-property mapping a store performs is applied in C# instead.
The read model definition Identifier, container and display name come straight off the CLR type and there are no indexes, where a deployed Chronicle uses your naming policy and the indexes you declared.
The client read surface With explicit seeds, scenario.ReadModels triggers the fold before instance reads and serves the refreshed instances. Other reads delegate to in-process services. Use Instance, Instances or InstanceForEventSourceId to assert directly on scenario output.

Old-generation events are migrated through the kernel’s event-type migrations before projection or reduction. Re-reading Instance after collecting more events folds the accumulated input again without duplicating its stored rows.

Some substitutions depend on the model’s keys, joins, or protection metadata. The scenario reports those separately so you can choose a companion integration test.

In .NET, Substitutions reports the substituted layers your read model’s own shape reaches. It is derived from the read model and its projection alone, so you can read it before seeding anything:

var scenario = new ReadModelScenario<OrderSummary>();
foreach (var substitution in scenario.Substitutions)
{
Console.WriteLine($"{substitution.Layer}: {substitution.Shape} — {substitution.Consequence}");
}

View C# snippet source on GitHub

Layer Reported when
Sink the document key’s underlying type is not a Guid, or the projection removes the root document through [RemovedWith] or [RemovedWithJoin]
Compliance the read model’s schema carries [PII] or [Encrypted] metadata, including metadata carried by a member’s concept type
JoinKeyResolution the read model or one of its child collections carries a [Join]
DeferredKeyHandling a child collection takes its parent key from an event property rather than from the event source id

For a protected read model, a correct value can conceal missing coverage. This harness reads and writes plaintext for both projections and reducers; it does not run the read-model encryption and release pipeline. A value assertion can check your mapping, but cannot show that the stored value is protected, that it belongs to the intended subject or scope, or what an erased PII subject reads back. Test those behaviors against a real kernel and store. Read models and PII explains the protection lifecycle.

An empty Substitutions list means no model-specific substitutions were detected. The general boundaries in the table above still apply: storage and observer delivery have not been tested.

Call WithStrictFidelity() when you want model-specific substitutions to fail the test rather than remain advisory. Reading a result then raises ReadModelDependsOnSubstitutedLayer if the model depends on a reported layer:

var scenario = new ReadModelScenario<OrderSummary>().WithStrictFidelity();

View C# snippet source on GitHub

Instance is null when no instance was seeded and the events produced no state. An explicitly seeded instance can be read without supplying events.

Each Given.ForEventSource(id).Events(...) call collects input for that source. ForEventSourceId(id) is an alias that also exposes .ReadModel(instance) for seeding state. Collect all the history needed for the assertion before reading a result.

The examples use Cratis Specifications assertions such as ShouldEqual. Keep your existing assertion library if you prefer; it does not change how the scenario folds events.

Once a projection or reducer produces the expected state, use a reactor scenario to check a reactor that consumes it. Seed that state explicitly there: reactor givens do not run this read-model fold.