Skip to content

Append assertions

Every Append and AppendMany call returns an IAppendResult. The Cratis.Chronicle.Testing package provides built-in Should* extension methods for asserting on these results.

Method Asserts
result.ShouldBeSuccessful() IsSuccess is true
result.ShouldBeFailed() IsSuccess is false
result.ShouldHaveConstraintViolations() HasConstraintViolations is true
result.ShouldNotHaveConstraintViolations() HasConstraintViolations is false
result.ShouldHaveConstraintViolationFor(name) A violation for the named constraint is present
result.ShouldHaveConstraintViolation(name) Singular alias for ShouldHaveConstraintViolationFor(name)
result.ShouldHaveConcurrencyViolations() HasConcurrencyViolations is true
result.ShouldNotHaveConcurrencyViolations() HasConcurrencyViolations is false
result.ShouldHaveErrors() HasErrors is true
result.ShouldNotHaveErrors() HasErrors is false

The success, violation, and error assertions check result flags, not collection counts. The named-constraint methods search ConstraintViolations for the supplied name.

All Should* methods throw AppendResultAssertionException on failure. Diagnostics depend on the method:

  • ShouldBeSuccessful() lists constraint violations and errors when their flags are set, and notes concurrency violations when flagged.
  • ShouldNotHaveConstraintViolations() lists constraint violations; ShouldNotHaveErrors() lists errors.
  • The named-constraint methods list actual constraint violations when HasConstraintViolations is set.
  • The remaining methods report the expected and actual flag state without listing individual violations or errors.
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
using Cratis.Chronicle.Testing.EventSequences;
[EventType]
public record TestingAssertionsAuthorRegistered([property: Unique(name: "TestingAssertionsUniqueAuthorName")] string Name);
public static class TestingAssertionsHappyPath
{
public static async Task Run()
{
var scenario = new EventScenario();
var authorId = EventSourceId.New();
var result = await scenario.EventLog.Append(authorId, new TestingAssertionsAuthorRegistered("Jane Smith"));
result.ShouldBeSuccessful();
}
}

View C# snippet source on GitHub

using Cratis.Chronicle.Events;
using Cratis.Chronicle.Testing.EventSequences;
public static class TestingAssertionsFailure
{
public static async Task Run()
{
var scenario = new EventScenario();
var authorId = EventSourceId.New();
var newAuthorId = EventSourceId.New();
await scenario.Given
.ForEventSource(authorId)
.Events(new TestingAssertionsAuthorRegistered("Jane Smith"));
var result = await scenario.EventLog.Append(newAuthorId, new TestingAssertionsAuthorRegistered("Jane Smith"));
result.ShouldBeFailed();
}
}

View C# snippet source on GitHub

Assert that a specific named constraint fired:

using Cratis.Chronicle.EventSequences;
public static class TestingAssertionsConstraintViolation
{
public static void Assert(AppendResult result)
{
result.ShouldHaveConstraintViolations();
result.ShouldHaveConstraintViolationFor("TestingAssertionsUniqueAuthorName");
}
}

View C# snippet source on GitHub

Assert that no constraint violations occurred:

using Cratis.Chronicle.EventSequences;
public static class TestingAssertionsNoConstraintViolation
{
public static void Assert(AppendResult result) => result.ShouldNotHaveConstraintViolations();
}

View C# snippet source on GitHub

using Cratis.Chronicle.EventSequences;
public static class TestingAssertionsConcurrency
{
public static void AssertHasViolations(AppendResult result) => result.ShouldHaveConcurrencyViolations();
public static void AssertNoViolations(AppendResult result) => result.ShouldNotHaveConcurrencyViolations();
}

View C# snippet source on GitHub

using Cratis.Chronicle.EventSequences;
public static class TestingAssertionsErrors
{
public static void AssertHasErrors(AppendResult result) => result.ShouldHaveErrors();
public static void AssertNoErrors(AppendResult result) => result.ShouldNotHaveErrors();
}

View C# snippet source on GitHub

For asserting on the event sequence itself (tail sequence number, event types, and event content), see Event Sequence Assertions.

Example: testing a constraint violation end-to-end

Section titled “Example: testing a constraint violation end-to-end”

Pre-seed state with Given, act with When, then assert on the AppendResult returned by When — including constraints declared on ConceptAs<T> properties:

using Cratis.Concepts;
using Cratis.Chronicle.Events;
using Cratis.Chronicle.Events.Constraints;
using Cratis.Chronicle.EventSequences;
using Cratis.Chronicle.Testing.EventSequences;
using Cratis.Specifications;
using Xunit;
// Email is a ConceptAs<string> — uniqueness on a domain value works in-process,
// so the constraint no longer needs to be hidden behind #if !DEBUG.
public record TestingAssertionsEmail(string Value) : ConceptAs<string>(Value);
[EventType]
public record TestingAssertionsAuthorRegisteredWithEmail([property: Unique("TestingAssertionsUniqueAuthorEmail")] TestingAssertionsEmail Email);
public class when_registering_an_author_with_a_taken_email : Specification, IDisposable
{
readonly EventScenario _scenario = new();
AppendResult _result = default!;
Task Establish() =>
_scenario.Given
.ForEventSource(EventSourceId.New())
.Events(new TestingAssertionsAuthorRegisteredWithEmail(new("john@doe.com")));
async Task Because() =>
_result = await _scenario.When
.ForEventSource(EventSourceId.New())
.Events(new TestingAssertionsAuthorRegisteredWithEmail(new("john@doe.com")));
[Fact] void should_be_rejected() =>
_result.ShouldBeFailed();
[Fact] void should_report_the_constraint() =>
_result.ShouldHaveConstraintViolation("TestingAssertionsUniqueAuthorEmail");
public void Dispose() => _scenario.Dispose();
}

View C# snippet source on GitHub