Skip to content

Cratis Chronicle: test a vertical slice

Test a Cratis Chronicle slice by checking the events its command appends and the read model those events produce. A slice is one path through a feature, from an action to its resulting state. In-process scenarios let you check those two parts without starting a Chronicle server or database.

The C# examples use Cratis.Chronicle.Testing. EventScenario runs the kernel append path with in-memory storage, including serialization and constraint validation. ReadModelScenario<TReadModel> runs the projection or reducer engine against supplied events. These are separate checks: appending through EventScenario does not start observers or update a read model for you.

The read-model harness also substitutes infrastructure such as projection storage and compliance services. It is not a running kernel with only its database replaced.

Terminal window
dotnet add package Cratis.Chronicle.Testing

The examples below use the Cratis Specifications style (Establish/Because/should_), but the scenarios work with any test framework.

Create an EventScenario, seed any pre-existing history with Given, and append through its EventLog. Assert on the AppendResult so the test checks whether the append was accepted, not just whether the call completed:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Testing.EventSequences;
using Cratis.Specifications;
using Xunit;
public record TestSliceBookId(Guid Value) : EventSourceId<Guid>(Value)
{
public static TestSliceBookId New() => new(Guid.NewGuid());
}
[EventType]
public record TestSliceBookAdded(string Title, [property: Unique(name: "TestSliceUniqueIsbn")] string Isbn);
[EventType]
public record TestSliceBookBorrowed(string BorrowedBy);
public class when_adding_a_book : Specification, IDisposable
{
EventScenario _scenario = null!;
AppendResult _result = null!;
void Establish() => _scenario = new EventScenario();
async Task Because() =>
_result = await _scenario.EventLog.Append(
TestSliceBookId.New(),
new TestSliceBookAdded("The Pragmatic Programmer", "978-0135957059"));
[Fact] void should_be_successful() => _result.ShouldBeSuccessful();
public void Dispose() => _scenario.Dispose();
}

View C# snippet source on GitHub

To test command behavior, pass the scenario’s EventLog to the command code under test. The direct append above checks the append path; it does not invoke your command for you.

Constraints are discovered from the configured client artifacts. With a unique-ISBN constraint such as the one in Enforce a unique value, seed a book and then append a duplicate. The expected result is a constraint violation:

using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Testing.EventSequences;
using Cratis.Specifications;
using Xunit;
public class and_the_isbn_already_exists : Specification, IDisposable
{
EventScenario _scenario = null!;
AppendResult _result = null!;
async Task Establish()
{
_scenario = new EventScenario();
await _scenario.Given
.ForEventSource(TestSliceBookId.New())
.Events(new TestSliceBookAdded("The Pragmatic Programmer", "978-0135957059"));
}
async Task Because() =>
_result = await _scenario.EventLog.Append(
TestSliceBookId.New(),
new TestSliceBookAdded("The Pragmatic Programmer, 2nd ed.", "978-0135957059"));
[Fact] void should_be_rejected() => _result.ShouldBeFailed();
[Fact] void should_report_the_violated_constraint() => _result.ShouldHaveConstraintViolationFor("TestSliceUniqueIsbn");
public void Dispose() => _scenario.Dispose();
}

View C# snippet source on GitHub

Use ShouldBeSuccessful/ShouldBeFailed for the overall outcome and ShouldHaveConstraintViolationFor to identify the rejected rule. Append result assertions also covers concurrency violations and errors. Create a fresh EventScenario per spec and dispose it; calls on the same instance share its accumulated history.

Test the read model with ReadModelScenario

Section titled “Test the read model with ReadModelScenario”

Use ReadModelScenario<TReadModel> to check how recorded facts become state. It detects whether a reducer, fluent projection or model-bound attributes build the model, then applies the supplied history through the corresponding engine:

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Keys;
using Cratis.Chronicle.Projections.ModelBound;
using Cratis.Chronicle.Testing.ReadModels;
using Cratis.Specifications;
using Xunit;
[FromEvent<TestSliceBookAdded>]
public record TestSliceBook(
[Key] Guid Id,
string Title,
[SetValue<TestSliceBookBorrowed>(true)] bool OnLoan,
[SetFrom<TestSliceBookBorrowed>(nameof(TestSliceBookBorrowed.BorrowedBy))] string? BorrowedBy);
public class when_a_book_is_borrowed : Specification
{
readonly TestSliceBookId _bookId = TestSliceBookId.New();
readonly ReadModelScenario<TestSliceBook> _scenario = new();
Task Because() =>
_scenario.Given
.ForEventSource(_bookId)
.Events(
new TestSliceBookAdded("The Pragmatic Programmer", "978-0135957059"),
new TestSliceBookBorrowed("Ada Lovelace"));
[Fact] void should_be_on_loan() => _scenario.Instance!.OnLoan.ShouldBeTrue();
[Fact] void should_record_the_borrower() => _scenario.Instance!.BorrowedBy.ShouldEqual("Ada Lovelace");
}

View C# snippet source on GitHub

Here Given supplies the events and Instance exposes the resulting model. The assertion checks that borrowing the book changes its availability. If it fails, inspect the supplied history, read model declaration and mapping, such as [SetFrom<T>]/[SetValue<T>] attributes or reducer logic. Using the real engine does not rule out a mistake in the test setup.

Together these checks establish what the exercised code appends and how the supplied facts become state. They do not establish that a deployed observer catches up, that the network connection works, or that a production sink behaves as expected. Use integration tests with a real kernel for those boundaries.

They also do not exercise your HTTP surface or UI. Test those through your application framework rather than treating an append or projection assertion as proof of the whole feature.

Continue with EventScenario or ReadModelScenario to configure initial state and dependencies. If the slice also reacts to events, Testing introduces ReactorScenario for checking those side effects.