Customizing
The one rule
A factory gives tests a valid district to start from. A test can customize that district when its behavior depends on a different value.
Changing when a teacher was invited should also change when the invitation expires by default. If the test supplies the expiry itself, that value should survive. Biome runs derived defaults after customizers to support both choices.
For every entity in an Add step, Biome runs the same pipeline, in this order:
- Factory creates the entity. IDs are set here with
ctx.NewId. - Traits (
.With) run, in declaration order. - Call-site customizers run.
Derives run, fill-only and in declaration order. Sequences first reserve every explicit value in the step, then fill the rest.- Global defaults (
options.Default) run, fill-only. - The entity is recorded, with its parent link and step.
There is no OnCreated hook that can overwrite a value. Steps 4 and 5 only fill properties that are still unset.
An input blueprint runs steps 2 to 4 on its input object (traits and customizers for every input in the step first, then each input's Derives followed by the factory), and then applies global defaults to the built entity (step 5).
Set only the property under test
A district report excludes archived districts. The setup creates one ordinary district and one archived district, so the reader can see which property drives the filter.
await TestBiome
.Start(_db, out _)
.AddDistrict(out var active)
// The report uses this flag to exclude the district.
.AddDistrict(out var archived, d => d.IsArchived = true)
.BuildAsync();
var now = DateTimeOffset.UtcNow;
now = new DateTimeOffset(now.Ticks - now.Ticks % 10, TimeSpan.Zero);
District NewDistrict(bool isArchived) =>
new()
{
Id = $"dist_{Guid.CreateVersion7():N}",
Name = "Riverside Unified",
TimeZoneId = "America/Chicago",
IsArchived = isArchived,
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
var active = NewDistrict(isArchived: false);
var archived = NewDistrict(isArchived: true);
_db.AddRange(active, archived);
await _db.SaveChangesAsync();
Traits: comma makes a new entity, plus stacks
If several tests make the same choice, give it a handle to make it reusable. A trait is an Action<T> value that you can pass anywhere a customizer goes.
The Quick start defines separate district traits for archiving and choosing a time zone.
public static class Districts
{
// Name common states so tests can reuse and combine them.
public static readonly Action<District> Archived = d => d.IsArchived = true;
public static readonly Action<District> Pacific = d => d.TimeZoneId = "America/Los_Angeles";
}
await TestBiome
.Start(out _)
.Add(
Blueprints.District,
out var districts,
// Both districts are archived. Only the second uses Pacific time.
Districts.Archived,
Districts.Archived + Districts.Pacific
)
.BuildAsync();
+ when one entity needs both traits, and a comma when the test needs separate entities.Bake a trait into a variant with .With. Each blueprint keeps its own traits.
// Make archiving the default for callers that choose this variant.
public static readonly Blueprint<SchoolSeed, District> ArchivedDistrict = District.With(
Districts.Archived
);
Context-aware customizers
Use two parameters when a customizer needs context. This test uses the shared clock to backdate a student’s enrollment.
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out _)
.AddOne(
Blueprints.Student,
out var returningStudent,
// Backdate enrollment relative to the clock shared by this scenario.
(student, ctx) => student.EnrolledAtUtc = ctx.Ago(TimeSpan.FromDays(30))
)
.BuildAsync();
Action<T> (or Action<TInput> for an input blueprint) unless the app has added a context overload for that type; otherwise a context-aware customizer goes through the core Add / AddOne.Derived defaults are fill-only
Some defaults depend on the entity's final values. A staff invitation expires seven days after it was created, so the expiry needs the creation time that remains after traits and customizers run. .Derive fills the expiry if it still has its default value.
public static readonly ChildBlueprint<SchoolSeed, School, StaffInvitation> StaffInvitation =
Blueprint
.ChildOf<SchoolSeed, School, StaffInvitation>(
(school, ctx) =>
new()
{
Id = ctx.NewId("invite"),
DistrictId = school.DistrictId,
SchoolId = school.Id,
Email = "teacher@example.test",
CreatedAtUtc = ctx.Now,
}
)
// Calculate expiry after the caller has had a chance to change the creation time.
.Derive(i => i.ExpiresAtUtc, (i, _) => i.CreatedAtUtc.AddDays(7));
var created = new DateTimeOffset(2026, 1, 1, 0, 0, 0, TimeSpan.Zero);
await TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.Add(
Blueprints.StaffInvitation,
out var invitations,
// The new creation time puts the default expiry on January 8. An explicit expiry wins.
i => i.CreatedAtUtc = created,
i =>
{
i.CreatedAtUtc = created;
i.ExpiresAtUtc = created.AddDays(1);
}
)
.BuildAsync();
What counts as "unset"
| Type | Unset means |
|---|---|
reference types, T? | null |
numbers, bool | 0, false |
DateTimeOffset | default(DateTimeOffset) |
string | null only. "" counts as set. |
null, so "" is kept as an explicit value. Leave derived strings null in the factory. If a test needs to retain null, use a base blueprint without that Derive.For nullable value types the distinction holds too: int? null fills, but 0 survives.
Derives run in order
The student blueprint uses the seat number in its default name. Derives run in declaration order, so assign the seat before reading it for the name.
// Assign the seat first so the name can use its number.
.Derive(s => s.Seat, Seats)
.Derive(s => s.FullName, (s, _) => $"Student {s.Seat}")
Properties must be settable
init property (CS8852), so a property that a test varies on the entity itself needs a set. If the model must stay init or required, keep production code as it is and customize an input instead: see Input blueprints. A blueprint factory method with optional named parameters also works, but it creates a new blueprint on every call.The property you select in Derive or options.Default must be a directly writable instance property, like e => e.Name. Anything else throws ArgumentException at declaration time. init properties are accepted here, because Derive and Default assign through a compiled setter.