Seeds and TestBiome
Constructing a seed context
The Quick start uses a time zone as the district baseline. If most tests also need a district and a school already in place, add those rows to the seed (TSeed). Biome passes the seed to each blueprint, and its shape can be a record, a string, or an existing fixture object.
public sealed record SchoolSeed(string TimeZoneId, District District, School School)
{
public static SchoolSeed Generate()
{
const string timeZoneId = "America/Chicago";
var now = DateTimeOffset.UtcNow;
var district = new District
{
Id = $"dist_{Guid.CreateVersion7():N}",
Name = "Riverside Unified",
TimeZoneId = timeZoneId,
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
var school = new School
{
Id = $"sch_{Guid.CreateVersion7():N}",
DistrictId = district.Id,
Name = "North High",
Code = "NH",
CreatedAtUtc = now,
UpdatedAtUtc = now,
};
// Blueprints read the time zone and tests can reuse the baseline rows.
return new(timeZoneId, district, school);
}
}
The seed is created outside the session for each test, so a new test never shares rows with another.
Seed rows
A seed can contain configuration as well as entities, so Biome cannot assume which objects belong in the database. Select those rows with SeedEntities. They are saved before the created entities, in the order you list them.
private static readonly BiomeOptions<SchoolSeed> Options =
// Save the district first because the school references it.
new BiomeOptions<SchoolSeed>().SeedEntities(seed => [seed.District, seed.School]);
Seed rows count as owned by the session, so a test can anchor on seed.School. A null entry in the list throws immediately.
DistrictId from the school in the class blueprint. ctx.Ancestor<District>() follows relationships created by Adds, so it can find the district when the school was built under an AddDistrict step.TestBiome: one call to start
Most tests use the same seed factory and options. Wrapping Biome.Start once gives them a short entry point. A test can then start from the school, add its classes, and save the baseline and classes together with one BuildAsync().
public static partial class TestBiome
{
private static readonly BiomeOptions<SchoolSeed> Options =
new BiomeOptions<SchoolSeed>().SeedEntities(seed => [seed.District, seed.School]);
public static BiomeBuilder<SchoolSeed, SchoolSeed> Start(
out SchoolSeed seed,
IBiomeStore? store = null,
TimeProvider? time = null
)
{
// Give each test its own baseline rows.
seed = SchoolSeed.Generate();
return Biome.Start(seed, Options, store, time);
}
public static BiomeBuilder<SchoolSeed, SchoolSeed> Start(
DbContext db,
out SchoolSeed seed,
TimeProvider? time = null
) =>
// Keep the same seed setup and use the test DbContext for saves.
Start(out seed, db.AsBiomeStore(), time);
}
await TestBiome
.Start(_db, out var seed)
// Use the seeded school as the parent of the new class.
.Under(seed.School)
.AddClass(out var schoolClass)
.BuildAsync();
var seed = SchoolSeed.Generate();
// Each test still wires baseline references into the rows it creates.
var schoolClass = new SchoolClass
{
Id = $"cls_{Guid.CreateVersion7():N}",
DistrictId = seed.District.Id,
SchoolId = seed.School.Id,
Name = "Algebra I",
Capacity = 25,
CreatedAtUtc = DateTimeOffset.UtcNow,
};
_db.AddRange(seed.District, seed.School, schoolClass);
await _db.SaveChangesAsync();
Biome.Start(seed, options, store, time) returns a BiomeBuilder<TSeed, TSeed>. A null store means NullStore. A null time means a frozen clock.
When several methods need the same starting graph, call TestBiome.Start in the test class's instance constructor and keep the baseline builder in an instance field. Each test instance gets a fresh session and extends it for its own scenario. See Constructor setup for TUnit and xUnit examples.
Options are shared and frozen
Sharing configuration is useful until one test changes it for another. Biome freezes options on first use and keeps each scenario's mutable state in its own session:
- Options are configured once, as a
static readonlyfield. - They freeze on the first
Start. Every later change (SeedEntities,Default,BeforeSave,AfterSave,TimePrecision,Ids) throwsInvalidOperationException. - Each
Startcreates a new session, so each test gets its own entities, sequences and clock. Parallel tests are safe.
If one test needs different options, use a factory that creates a fresh instance. Update the options field and the store-based overload as shown below.
public static partial class TestBiome
{
// Give tests their own options when they need to add hooks.
public static BiomeOptions<SchoolSeed> NewOptions() =>
new BiomeOptions<SchoolSeed>().SeedEntities(seed => [seed.District, seed.School]);
private static readonly BiomeOptions<SchoolSeed> Options = NewOptions();
public static BiomeBuilder<SchoolSeed, SchoolSeed> Start(
out SchoolSeed seed,
IBiomeStore? store = null,
TimeProvider? time = null,
BiomeOptions<SchoolSeed>? options = null
)
{
seed = SchoolSeed.Generate();
// Use the shared baseline when the test supplies no options.
return Biome.Start(seed, options ?? Options, store, time);
}
}
A test can pass in TestBiome.NewOptions().BeforeSave(...) while the others keep the shared options. See Custom seed.
A seed with timestamps
ctx.Now. If seed timestamps are compared with database values, round them yourself, or set them from the same TimeProvider you pass to Start.An existing school fixture can also serve as the seed:
// Reuse the fixture’s existing list of database rows.
new BiomeOptions<SchoolFixtureData>().SeedEntities(seed => seed.GetSeedEntities());