Koto.Testing
0.5.0-preview.4
See the version list below for details.
dotnet add package Koto.Testing --version 0.5.0-preview.4
NuGet\Install-Package Koto.Testing -Version 0.5.0-preview.4
<PackageReference Include="Koto.Testing" Version="0.5.0-preview.4" />
<PackageVersion Include="Koto.Testing" Version="0.5.0-preview.4" />
<PackageReference Include="Koto.Testing" />
paket add Koto.Testing --version 0.5.0-preview.4
#r "nuget: Koto.Testing, 0.5.0-preview.4"
#:package Koto.Testing@0.5.0-preview.4
#addin nuget:?package=Koto.Testing&version=0.5.0-preview.4&prerelease
#tool nuget:?package=Koto.Testing&version=0.5.0-preview.4&prerelease
Koto.Testing
Tests that read as the rules of the domain, and one-line tests that keep the structure of a service honest. Built on AwesomeAssertions. Part of the Koto suite.
Install
dotnet add package Koto.Testing --prerelease
What's included
| Type | Purpose |
|---|---|
Should() on Result, Result<T>, AppResult, AppResult<T> |
BeSuccess(), BeFailureWith<TError>(), HaveError<TError>(), HaveValue(x), HaveErrorForField("email"); by code: BeFailureWith("code"), HaveErrors("a", "b") |
Should() on an aggregate |
HaveRaised<TEvent>(), NotHaveRaised<TEvent>(), HaveRaisedNothing() |
Given.History<TAggregate>(events…) |
An event-sourced aggregate rebuilt from its past |
Layers |
DomainShouldBePure, DomainNamespaceShouldBePure, ShouldNotReference |
ErrorCatalog |
ShouldBeValid(assemblies) — every error constructs, every code is well-formed and unique; Scan(assemblies) lists them |
DomainMap |
The domain as Markdown, generated from the code and kept current by a test |
FakeRepository<TAgg, TId> / FakeIntegrationEventPublisher |
In-memory stand-ins for handler tests |
Namespaces: Koto.Testing.Assertions, .Aggregates, .Architecture, .Catalog, .Fakes.
The rules as tests
A rule is named by its error type; the error's data is right there to assert.
[Fact]
public void An_order_can_be_cancelled_for_thirty_minutes()
{
var order = OrderPlacedAtNoon();
order.Cancel("Changed my mind", now: Noon.AddMinutes(30)).Should().BeSuccess();
order.IsCancelled.Should().BeTrue();
order.Should().HaveRaised<OrderCancelledDomainEvent>().Which.Reason.Should().Be("Changed my mind");
}
[Fact]
public void An_order_cannot_be_cancelled_once_the_window_has_closed()
{
var order = OrderPlacedAtNoon();
order.Cancel("Too late", now: Noon.AddMinutes(31)).Should().BeFailureWith<CancellationWindowClosed>()
.Which.ClosedAt.Should().Be(Noon.AddMinutes(30));
order.IsCancelled.Should().BeFalse();
order.Should().NotHaveRaised<OrderCancelledDomainEvent>();
}
[Fact]
public void An_order_cannot_be_cancelled_twice()
{
var order = OrderPlacedAtNoon();
order.Cancel("Changed my mind", Noon).Should().BeSuccess();
order.Cancel("Again", Noon).Should().BeFailureWith<OrderAlreadyCancelled>()
.Which.OrderId.Should().Be(order.Id);
}
BeFailureWith<TError>()checks the first error,HaveError<TError>()any of them;.Whichis the error.- On a handler's
AppResultthe domain error is found inside theEnrichedErrorthat carries it, so the same assertion works one layer up:result.Should().BeFailureWith<OrderNotFound>(). aggregate.Should()keeps the usual object assertions (NotBeNull,Be,BeSameAs).- A rejected operation should leave nothing behind:
order.Should().HaveRaisedNothing().
An event-sourced aggregate
Given its history, do something, look at the outcome and at what it raised.
var account = Given.History<Account>(new AccountOpened(id), new CoinsDeposited(10));
account.Withdraw(15).Should().BeFailureWith<InsufficientFunds>()
.Which.Should().Be(new InsufficientFunds(Available: 10, Requested: 15));
account.Should().HaveRaisedNothing();
The structure of the service
[Fact]
public void The_domain_depends_on_nothing_but_Koto_Domain() =>
Layers.DomainShouldBePure(typeof(Order).Assembly);
[Fact]
public void The_use_cases_know_no_infrastructure() =>
Layers.ShouldNotReference(typeof(PlaceOrderHandler).Assembly, Layers.Infrastructure);
[Fact]
public void Every_error_has_a_valid_and_unique_code() =>
ErrorCatalog.ShouldBeValid(typeof(Order).Assembly, typeof(PlaceOrderHandler).Assembly);
Layers.DomainShouldBePure(assembly)— the domain references only the base class library andKoto.Domain. Storage, HTTP and JSON parts of the base class library count as infrastructure. An exception made on purpose is named in the test:DomainShouldBePure(assembly, "MyCompany.SharedKernel").Layers.DomainNamespaceShouldBePure(assembly, "MyService.Domain")— the same for a domain that shares its project with other layers. It reads type signatures, not method bodies; a domain in its own project gets the full guarantee.Layers.ShouldNotReference(assembly, Layers.Infrastructure)— no EF Core, Npgsql, Marten, Wolverine, Kafka, ASP.NET Core or HTTP client in the use cases.Layers.Infrastructureis a plain array: extend or trim it.
The domain map
[Fact]
public void The_domain_map_is_up_to_date() =>
DomainMap.ShouldBeUpToDate("../docs/domain-map.md", [typeof(Order).Assembly, typeof(PlaceOrderHandler).Assembly]);
The test writes docs/domain-map.md from the code — aggregates with their operations and the numbers
their rules use, value objects, events, the error catalog, commands and queries. When the code changes,
the test rewrites the file and fails once, so the change to the map lands in the same commit.
An example: samples/Shop/docs/domain-map.md.
Summaries are quoted from XML documentation when the project generates it
(<GenerateDocumentationFile>true</GenerateDocumentationFile>).
A handler with fakes
sealed class FakeOrders : FakeRepository<Order, OrderId>, IOrderRepository;
var orders = new FakeOrders();
orders.Add(order);
var result = await new CancelOrderHandler(orders, clock).HandleAsync(new CancelOrderCommand(order.Id.Value, "Changed my mind"));
result.Should().BeSuccess();
FakeIntegrationEventPublisher captures what was published: publisher.GetPublishedEvent<OrderPlacedV1>().
Upgrading
Within 0.5 (preview.2): AggregateTestFixture, AggregateAssertions (ShouldHaveRaised…) and
IAggregateApply<TEvent> are gone. Call the aggregate directly and assert on the result and on the
aggregate; rebuild an event-sourced aggregate with Given.History. Guide:
docs/migrations/0.4-to-0.5.md.
License
MIT
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net10.0
- AwesomeAssertions (>= 9.4.0)
- Koto.Application (>= 0.5.0-preview.4)
- Koto.Domain (>= 0.5.0-preview.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
| Version | Downloads | Last Updated |
|---|---|---|
| 0.5.0-preview.9 | 47 | 11/30/2026 |
| 0.5.0-preview.8 | 54 | 11/30/2026 |
| 0.5.0-preview.7 | 55 | 11/29/2026 |
| 0.5.0-preview.6 | 52 | 11/29/2026 |
| 0.5.0-preview.5 | 52 | 11/29/2026 |
| 0.5.0-preview.4 | 52 | 11/29/2026 |
| 0.5.0-preview.3 | 50 | 11/29/2026 |
| 0.5.0-preview.2 | 53 | 11/29/2026 |
| 0.5.0-preview.1 | 63 | 11/8/2026 |
| 0.4.0 | 166 | 7/21/2026 |
| 0.3.0-preview.15 | 91 | 7/21/2026 |
| 0.3.0-preview.14 | 85 | 7/21/2026 |
| 0.3.0-preview.13 | 87 | 7/21/2026 |
| 0.3.0-preview.12 | 82 | 7/21/2026 |
| 0.3.0-preview.11 | 84 | 7/21/2026 |
| 0.3.0-preview.10 | 73 | 7/21/2026 |
| 0.3.0-preview.9 | 91 | 7/21/2026 |
| 0.3.0-preview.8 | 93 | 7/21/2026 |
| 0.3.0-preview.7 | 87 | 7/21/2026 |
| 0.3.0-preview.6 | 78 | 7/20/2026 |
0.5.0-preview.4 — a value object's factory parses and never answers "required": MustBeValueObject reports a missing value itself (general.value.is-required, bound to the field) and does not call the factory with null. Remove "required" errors from value-object factories. Since preview.3: analyzers KOTO001 (a result is discarded) and KOTO002 (FirstError returned as the failure) are errors. Migration guide: https://github.com/arigatory/Koto/blob/main/docs/migrations/0.4-to-0.5.md