Skip to content

Spec-driven development with Cratis

Spec-driven development with Cratis starts by describing a feature in an event model and Given/When/Then examples before implementing it. Those examples state the intended behavior for developers and coding agents. Screenplay checks supported model scenarios in memory; code-side specifications check the implementation separately.

Suppose registration must record an author’s name. Before choosing a handler or a projection, agree on the example: when a new author registers, which event is recorded, and what should the authors screen show? That gives reviewers something concrete to compare with the code, rather than reconstructing the agreement from a diff.

An event model shows the command, the facts it records and the state a screen reads. Given/When/Then turns that flow into an example: given earlier facts, when a command arrives, then these outcomes should follow.

Writing the example first exposes unanswered questions before implementation. Running it later checks the behavior it actually asserts, not every possible behavior of the feature. That distinction matters when a coding agent supplies the implementation: passing specs are review evidence, not permission to skip review.

Each step produces something the next step reads. You can enter the chain at any point; Arc and Chronicle do not depend on the model-first tools.

Step What you have Where it is documented
1. Event model Slices: a screen, its command, the event, the read model behind the next screen Event modeling, Studio
2. Screenplay specifications specification blocks inside a slice in a .play file, compiled and run by the Screenplay tool for supported scenarios Screenplay
3. Stage Compiles .play folders, checks supported declared specifications, and renders reviewable Arc and Chronicle code for supported slice shapes Stage
4. Arc and Chronicle code The command, event and projection of the slice Build a full app, capability map
5. Code-side specs and replay Executable behavior-driven development (BDD) specs in your backend’s test framework, plus Chronicle replay of read models Testing with Cratis; Specifications for the .NET lifecycle
Alternative start Prologue captures signals from an existing system and proposes a model for you to review Prologue

The specifications appear twice on purpose. The Screenplay specification checks the model: given these events, when this command, the model says these events follow. The code-side test checks the code: the implementation really appends them. Keeping both lets you check the modeled behavior separately from its implementation. A passing model scenario does not establish that the business rule itself is correct.

Take the column from the testing guide: a screen registers an author, the RegisterAuthor command appends AuthorRegistered, and the Author read model feeds the authors screen.

In Screenplay, the specification lives inside the slice next to the command and the event. This follows the shape of the examples in the Screenplay specifications page, reduced to an author identifier and a name property. The identifier tells Screenplay which event source the event belongs to, the same role the author id plays in the commands below:

module Authors
feature Registration
slice StateChange RegisterAuthor
command RegisterAuthor
authorId Uuid identifier
name String
produces AuthorRegistered
for authorId
name = name
event AuthorRegistered
name String
specification RegisteringANewAuthor
when RegisterAuthor
authorId = "0c9d5a8e-3b8f-4a52-9a1e-6f1d2c3b4a5e"
name = "Jane Austen"
then AuthorRegistered
name = "Jane Austen"

Run it with the Screenplay tool, which evaluates the model in memory without rendering an application or starting services:

Terminal window
screenplay test path/to/screenplays

The same behavior on the code side is a command spec. With the Arc backend’s Chronicle integration configured, the handler returns the event and Arc appends it for the author id. These excerpts show the command and event, not the application’s host setup:

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 spec executes that command through Arc’s command pipeline and checks the recorded fact. Choose the same backend as above:

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

No HTTP or Chronicle server starts in these tests: each backend’s test support captures appended events in memory. The model-level and code-side specifications describe the same intended behavior at different levels; passing either one does not prove they agree.

In C#, Because() is the when and each [Fact] is a then; there is no Establish() because this registration needs no prior facts. TypeScript uses Vitest setup and assertions, while Kotlin and Java use JUnit. The JVM appended-event helper expects exactly one matching event; C# and TypeScript accept one or more. TypeScript’s scenario does not enforce constraints. Testing with Cratis explains these limits and adds the read-model spec; Specifications covers the .NET lifecycle methods and folder naming.

Give the agent a reviewed example rather than asking it to infer the rule from a feature name:

  1. Supply the .play file or Given/When/Then table and identify the code-side specifications it should satisfy.
  2. Ask it to implement the behavior and run the specifications, without changing their expected outcomes to fit its code.
  3. Review changes to the specifications as changes of intent. Review implementation changes against those examples and the behavior they leave untested.

Cratis AI installs rules and skills into a repository so an agent follows the conventions of the stack, including the specification style; see AI-native development and the EventModelers.ai workflow. For how agents fit into a whole delivery process, read agentic development.

Check the capability map before choosing tools. Stage’s specification runner checks declared scenarios against modeled facts and writes results.json; it does not exercise every slice through a live generated application. Unsupported constructs are not passing scenarios. Operation specifications are syntax only and cannot be executed by the reference runner.

Stage renders a subset of slice shapes, and generated output needs review. Ordinary Given/When/Then examples also do not prove concurrency handling. Decisions that hold explains the read protection that guards an append when decision state changes.

If your next task is to test application code, start at step 5. The Arc and Chronicle testing scenarios work without the model-first tools.

Spec-driven development is a way of building software where the specification is written first and stays the contract. In Cratis the specification is the event model plus Given/When/Then specifications. Code is written or generated against them and checked by running them.

Is this the same as test-driven development?

Section titled “Is this the same as test-driven development?”

They overlap. Test-driven development writes a failing test before the code, usually in the language of the implementation. Here the spec is written one level earlier, in the event model and in Screenplay, in terms a domain expert can read. The executable code-side specs are then the test-first part.

No. The code side works alone: Arc command scenarios and supported Chronicle in-process scenarios need none of the model-first tools. Screenplay and Stage add a model-level spec and, for supported shapes, generated code.

Which languages can run the specifications?

Section titled “Which languages can run the specifications?”

The testing guide shows command and read-model specs for the Arc and Chronicle client languages, with differences between backends, and lists what Elixir does not support yet. The Specifications library itself targets .NET with xUnit or NUnit. See Testing with Cratis.

Can a coding agent write the specifications too?

Section titled “Can a coding agent write the specifications too?”

It can draft them, but a person should approve them. A spec an agent writes for its own code only proves the code does what the agent wrote. Have the agent propose the specification from the event model, review it as intent, then let it implement.

For a throwaway prototype, a pure infrastructure change, or a very small assertion, a full model and specification adds more than it returns. The Specifications page lists where plain xUnit or NUnit tests are the better choice.