Blueprints
Writing a valid entity often takes more properties than the test actually cares about. A blueprint keeps those routine choices in one factory, leaving the test free to minimally describe its scenario.
Declare it as a static readonly value with valid defaults. Choose among these forms based on the relationship you need:
| Kind | Factory | Use for |
|---|---|---|
Blueprint<TSeed, T> | Blueprint.For<TSeed, T>(ctx => new() { … }) | A root entity |
ChildBlueprint<TSeed, TParent, T> | Blueprint.ChildOf<TSeed, TParent, T>((parent, ctx) => new() { … }) | An entity that belongs to a parent |
InputBlueprint<TSeed, TInput, T> | Blueprint.FromInput<TSeed, TInput, T>(() => new(), (input, ctx) => …) | A root entity (or group of rows) built from a customizable input |
ChildInputBlueprint<TSeed, TParent, TInput, T> | Blueprint.ChildFromInput<TSeed, TParent, TInput, T>(() => new(), (parent, input, ctx) => …) | A child built from a customizable input |
The two input forms are for rows that tests cannot edit after construction, such as init-only properties. See Input blueprints and check that the package referenced by your project exposes the form you use.
A root blueprint
A district needs a time zone and timestamps even in a test about its schools. Put those choices in the district blueprint so every test starts with a valid tenant.
public static partial class Blueprints
{
public static readonly Blueprint<SchoolSeed, District> District = Blueprint.For<
SchoolSeed,
District
>(ctx =>
new()
{
Id = ctx.NewId("dist"), // Honor the session’s configured ID generator.
Name = "Riverside Unified",
// Use the baseline time zone and the session’s precision-rounded clock.
TimeZoneId = ctx.Seed.TimeZoneId,
CreatedAtUtc = ctx.Now,
UpdatedAtUtc = ctx.Now,
}
);
}
public static class TestData
{
public static District District(
string timeZoneId,
TimeProvider time,
string name = "Riverside Unified"
)
{
var now = time.GetUtcNow();
// Truncate to microseconds so timestamps survive a Postgres round trip.
now = new DateTimeOffset(now.Ticks - now.Ticks % 10, TimeSpan.Zero);
return new District
{
Id = $"dist_{Guid.CreateVersion7():N}",
Name = name,
TimeZoneId = timeZoneId,
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
}
}
The factory receives a context (ctx):
| Member | Use |
|---|---|
ctx.Seed | The baseline, such as the district time zone or seed rows. |
ctx.Now, ctx.Ago(t), ctx.FromNow(t) | The session clock, already rounded. |
ctx.NewId("prefix") | prefix_<uuidv7> unless options override it. |
ctx.Index | Position in this Add. Cosmetic values only. |
ctx.Next(seq) | Advance a sequence manually. Prefer Derive(prop, seq). |
ctx.Ancestor<T>() | Nearest ancestor of type T. Throws a clear error if none. |
ctx.All<T>(), ctx.Last<T>() | Inspection only. Never for wiring relationships. |
ctx.Parent, ctx.ParentIndex | Child context only. |
Factory rules
A useful factory makes ordinary entities easy to build without choosing the behavior under test. These conventions keep defaults predictable as the test suite grows:
- Valid and uninteresting. Every required column gets a default no test should care about.
- IDs come from
ctx.NewId("prefix"). Time comes fromctx.Now/ctx.Ago/ctx.FromNow. - Take the parent from the typed argument, and grandparents from
ctx.Ancestor<T>(). - Leave derived properties unset, so a
Derivecan fill them. - A property that tests vary must be settable, or come from an input that the factory reads.
.Under make that assumption unreliable. Use the typed parent for relationships, and keep ctx.Last<T>() and ctx.All<T>() for inspection.{ get; init; } the compiler rejects the assignment (CS8852). Derive can still fill init properties. If the model must stay immutable, let tests customize an input blueprint instead of changing the model.Blueprints are immutable values
Tests often need a recurring variation, such as an archived district. A variant lets you name that choice without changing the defaults other tests use. Every modifier returns a new blueprint:
// Keep the ordinary district defaults available to other tests.
public static readonly Blueprint<SchoolSeed, District> ArchivedDistrict = District.With(
Districts.Archived
);
| Modifier | Effect |
|---|---|
.With(trait) | Adds a trait. Runs before call-site customizers, so the call site still wins. |
.Derive(e => e.Prop, (e, ctx) => value) | Fill-only assignment after customizers. |
.Derive(e => e.Prop, sequence) | Fill-only assignment from a sequence. |
.AsTransient() | The output is built and returned, but never saved. See Transients. |
partial files is unspecified. Put a sequence above the blueprint that uses it for the same reason.Adding blueprints to a chain
// Use the blueprint directly when there is no generated graft.
await TestBiome.Start(out _).AddOne(Blueprints.District, out var district).BuildAsync();
In real tests you rarely call AddOne directly. Mark the blueprint catalog [BiomeGraft] and the generator supplies methods such as .AddDistrict(out var district). See Generated grafts and scenario helpers.