Skip to content

Testing with Cratis

Testing

Turn the model into executable examples

When a user registers an author, two things need checking: the command records the author’s name, and the authors screen can read it. Cratis scenarios let you test those behaviors in process, without starting a Chronicle server. Supply earlier events as the context, execute the command or feed an event to a read model, then assert the outcome.

Choose the packages for your application language. The command examples use Arc’s command pipeline; read-model examples use a scenario or call a reducer directly, as listed below. Neither requires a Chronicle server, but application services used by your handler still need test implementations or configured dependencies.

Backend Command specs Read-model specs
C# Cratis.Arc.Testing (CommandScenario<TCommand>), with Cratis.Arc.Chronicle.Testing for the events a command appends Cratis.Chronicle.Testing (ReadModelScenario<TReadModel>)
Kotlin io.cratis:arc-testing (CommandScenario), with io.cratis:arc-chronicle-spring-boot-starter for the events a command appends io.cratis:chronicle-testing (ReadModelScenario)
Java io.cratis:arc-testing, driven through BlockingCommandScenario, with the same Chronicle starter The examples call the reducer directly
TypeScript @cratis/arc.testing (CommandScenario), with @cratis/arc.chronicle/testing (ChronicleCommandScenario) for the events a command appends @cratis/chronicle/testing (ReadModelScenario)

The Arc for Kotlin and Java testing guide and the Arc for TypeScript testing guide cover the JVM and TypeScript harnesses in depth. The rest of this section lists the .NET packages.

For .NET, the packages are layered the same way the runtime is layered:

Package Use it for
Cratis.Specifications.XUnit Behavior-driven development (BDD) specifications on top of xUnit: Establish() for given, Because() for when, [Fact] methods for then, plus Should* assertions and Catch.Exception.
Cratis.Specifications.NUnit The same specification style for NUnit projects.
Cratis.Arc.Testing CommandScenario<TCommand> for running Arc commands through the real command pipeline without HTTP.
Cratis.Chronicle.Testing EventScenario, ReadModelScenario<TReadModel>, and ReactorScenario<TReactor> for in-process Chronicle tests without a server or database.
Cratis.Arc.Chronicle.Testing Automatically extends CommandScenario<TCommand> with an in-memory Chronicle event log when the package is referenced.
Cratis.Testing Convenience package for Arc + Chronicle application tests. Use it with Cratis.Specifications.XUnit.

For most .NET Cratis stack slices, reference:

<PackageReference Include="Cratis.Specifications.XUnit" />
<PackageReference Include="Cratis.Testing" />

Use Cratis.Chronicle.Testing directly for Chronicle-only libraries, and Cratis.Arc.Testing directly when a slice deliberately has no event-sourced write side.

Behavior-driven development (BDD) describes a test as given/when/then. For an event-sourced feature, the given is the facts that already happened, the when is the command or event under test, and the then is the facts, read models, or side effects that result. The .NET specification APIs line up like this:

Specification word Event-sourced meaning Cratis test API
Given Facts that already happened, or read model state that already exists EventScenario.Given, ReadModelScenario.Given, reusable given base contexts
When The command, append, projection input, or reactor trigger under test CommandScenario.Execute, EventLog.Append, ReactorScenario.Given.ForEventSource(...).Events(...)
Then The observable result: appended events, command result, projected read model, or side effect [Fact] methods with ShouldBeSuccessful, ShouldHaveAppendedEvent, ShouldEqual, or mock assertions

That makes specs readable in the same language as the event model. A domain expert can read “given an author already exists, when registering the same name, then it should not append another event” and understand the rule without knowing the implementation.

Start with one column from an event model:

UI/A: AuthorsUI/A: AuthorsC/RM: AuthorsC/RM: AuthorsStream: Authors
RegisterAuthorScreen
RegisterAuthor

authorId: uuid, name: string
AuthorRegistered

name: string
Author
AuthorsScreen

Read it as a testable behavior:

Given When Then
no author exists for this id RegisterAuthor is executed the command succeeds
no author exists for this id RegisterAuthor is executed AuthorRegistered is appended for the author id
AuthorRegistered exists the projection handles it the Author read model contains the name the screen needs

The command’s handler returns the event, and Arc appends it to the event source the command identifies. The slice stays small: each snippet holds only the RegisterAuthor command and the AuthorRegistered event. The Author read model from the event model comes in with the spec under Spec the read side.

using Cratis.Arc.Commands.ModelBound;
using Cratis.Chronicle.Events;
namespace Library.Authors;
[Command]
public record RegisterAuthor(EventSourceId AuthorId, string Name)
{
public AuthorRegistered Handle() => new(Name);
}
[EventType]
public record AuthorRegistered(string Name);

View C# snippet source on GitHub

The command spec covers the first two rows: the command succeeds, and AuthorRegistered is recorded for the author id.

using Cratis.Arc.Chronicle.Testing.Commands;
using Cratis.Arc.Commands;
using Cratis.Arc.Testing.Commands;
using Cratis.Chronicle.Events;
using Cratis.Specifications;
using Xunit;
namespace Library.Authors.for_RegisterAuthor;
public class when_registering_a_new_author : Specification
{
readonly EventSourceId _authorId = EventSourceId.New();
readonly CommandScenario<RegisterAuthor> _scenario = new();
CommandResult _result = default!;
async Task Because() => _result = await _scenario.Execute(new RegisterAuthor(_authorId, "Jane Austen"));
[Fact] void should_accept_the_command() => _result.ShouldBeSuccessful();
[Fact] Task should_record_the_fact() =>
_scenario.ShouldHaveAppendedEvent<RegisterAuthor, AuthorRegistered>(
_authorId,
e => e.Name == "Jane Austen");
void Destroy() => _scenario.Dispose();
}

View C# snippet source on GitHub

There is no HTTP server in that spec and no Chronicle server to start. Arc runs the command through the real command pipeline, and each backend’s Chronicle test support captures the events the command appends in memory. In Kotlin and Java, shouldHaveAppendedEvent expects exactly one matching event; C# (ShouldHaveAppendedEvent) and TypeScript accept one or more. TypeScript’s ChronicleCommandScenario does not enforce constraints; the Arc for TypeScript testing guide lists what it does not cover.

The read-side spec covers the last row: given AuthorRegistered, the Author read model contains the name the screen needs. C#, Kotlin, and TypeScript run the read model in process with a read-model scenario; Java calls its reducer directly. Elixir does not support this test yet. Each example declares AuthorRegistered again so it compiles on its own (in your application, reuse the event from the slice) and introduces the Author read model. These tabs are the Chronicle client set, which also includes Elixir. They are separate from the Arc backend tabs above, so picking a backend there does not switch them.

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 AuthorRegistered(string Name);
[FromEvent<AuthorRegistered>]
public record Author([Key] Guid Id, string Name);
public class when_projecting_a_registered_author : Specification
{
readonly EventSourceId _authorId = EventSourceId.New();
readonly ReadModelScenario<Author> _scenario = new();
Task Because() =>
_scenario.Given
.ForEventSource(_authorId)
.Events(new AuthorRegistered("Jane Austen"));
[Fact] void should_set_the_author_name() =>
_scenario.Instance!.Name.ShouldEqual("Jane Austen");
}

View C# snippet source on GitHub

In C#, one spec can cover both sides: pass the contents of the command scenario’s AppendedEvents to ReadModelScenario<TReadModel>.Given.ForEventSource(...).Events(...), and the last assertion checks the same state the React query reads.

Use Arc tests for command behavior: validation, authorization, injected services, and whether the command succeeds. These examples register a service before executing a command that calls it through the Arc pipeline:

using System.Threading.Tasks;
using Cratis.Arc.Commands;
using Cratis.Arc.Testing.Commands;
using Cratis.Specifications;
using Library.Authors;
using Microsoft.Extensions.DependencyInjection;
using NSubstitute;
using Xunit;
namespace Library.Specs;
public class when_recording_an_author : Specification
{
readonly CommandScenario<RecordAuthor> _scenario = new();
readonly AuthorId _id = AuthorId.New();
readonly AuthorName _name = new("Ada Lovelace");
IAuthorRegistration _registration = null!;
CommandResult _result = null!;
void Establish()
{
_registration = Substitute.For<IAuthorRegistration>();
_registration.Register(_id, _name).Returns(Task.CompletedTask);
_scenario.Services.AddSingleton(_registration);
}
async Task Because() => _result = await _scenario.Execute(new RecordAuthor(_id, _name));
[Fact] void should_succeed() => _result.ShouldBeSuccessful();
[Fact] async Task should_register_the_author() => await _registration.Received(1).Register(_id, _name);
void Destroy() => _scenario.Dispose();
}

View C# snippet source on GitHub

In .NET, use Validate() when you want validation and authorization without running the handler. Use the Chronicle extension when the command returns events and you want to assert which facts were recorded.

For .NET tests, choose the scenario for the part of the event pipeline your code owns:

You need to prove Use
Code appends the right event, or rejects an append because of constraints EventScenario
A projection or reducer builds the read model correctly ReadModelScenario<TReadModel>
A reactor performs the right side effect when an event arrives ReactorScenario<TReactor>

Use a real Chronicle integration test when you are testing hosting, storage, subscriptions, observer recovery, or the boundary between processes. In .NET, Kotlin, and TypeScript, keep supported read-model behavior in the in-process scenarios so the suite stays fast.

A full .NET slice usually has three useful spec levels:

Level What it proves Typical tool
Command spec The command validates, authorizes, and records the right facts CommandScenario<TCommand> + Cratis.Arc.Chronicle.Testing
Read side spec The event history builds the read model the screen needs ReadModelScenario<TReadModel>
Workflow spec The app boundary, identity, tenant routing, real Chronicle host, and projections work together xUnit fixture + Cratis.Specifications.XUnit + HTTP/client helpers

Start with command and read-side specs for every meaningful event-model column. Add workflow specs around the edges where the wiring matters: AuthProxy headers, tenant isolation, a real Chronicle server, or browser-visible behavior.

For a first spec, keep the author-registration example’s boundary: assert the command’s event, then check the state built from that event. Follow the Arc or Chronicle testing guide below when you need service setup, constraints or reactor assertions.