Constructor setup
A fresh baseline for each test
Several roster tests might start with the same district, school and class. Repeating that chain in every method obscures the part of the scenario that differs. Put the common starting graph in the instance constructor, then let each test add the rows and properties its behavior needs.
Both TUnit and xUnit create a fresh test class instance for each test. The constructor calls Start for that instance, so every test gets its own seed, session, entities and sequence state. The methods reuse the setup recipe; each execution builds a new graph.
Keep the baseline builder in an instance field. Leave the session open in the constructor, extend it in the test, and call BuildAsync after the test's full setup is ready.
Constructor and test continuations
The roster tests use the entities and grafts from the quick start. The constructor ends with AddClass. Even a singular Add returns a builder anchored to its output array, so the field's anchor type is SchoolClass[].
using TUnit.Assertions;
using TUnit.Core;
using Zeeq.Biome;
public sealed class ClassRosterTests
{
private readonly BiomeBuilder<SchoolSeed, SchoolClass[]> _baseline;
private readonly SchoolClass _schoolClass;
public ClassRosterTests()
{
// Start a fresh seat sequence for each test instance.
_baseline = TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out _schoolClass);
}
[Test]
public async Task First_student_takes_seat_one()
{
// Add the first student to the constructor's class before completing setup.
await _baseline.AddStudent(out var student).BuildAsync();
await Assert.That(student.ClassId).IsEqualTo(_schoolClass.Id);
await Assert.That(student.Seat).IsEqualTo(1);
}
[Test]
public async Task Two_students_take_consecutive_seats()
{
// This test's class has its own sequence, so the first seat is still 1.
await _baseline.AddStudents(out var students, 2).BuildAsync();
await Assert.That(students[0].Seat).IsEqualTo(1);
await Assert.That(students[1].Seat).IsEqualTo(2);
}
}
using Xunit;
using Zeeq.Biome;
public sealed class ClassRosterTests
{
private readonly BiomeBuilder<SchoolSeed, SchoolClass[]> _baseline;
private readonly SchoolClass _schoolClass;
public ClassRosterTests()
{
// Start a fresh seat sequence for each test instance.
_baseline = TestBiome
.Start(out _)
.AddDistrict(out _)
.AddSchool(out _)
.AddClass(out _schoolClass);
}
[Fact]
public async Task First_student_takes_seat_one()
{
// Add the first student to the constructor's class before completing setup.
await _baseline.AddStudent(out var student).BuildAsync();
Assert.Equal(_schoolClass.Id, student.ClassId);
Assert.Equal(1, student.Seat);
}
[Fact]
public async Task Two_students_take_consecutive_seats()
{
// This test's class has its own sequence, so the first seat is still 1.
await _baseline.AddStudents(out var students, 2).BuildAsync();
Assert.Equal(1, students[0].Seat);
Assert.Equal(2, students[1].Seat);
}
}
Both tests start at seat 1. The constructor creates a fresh class and sequence for each one, and the test decides how many students to add. In a behavior test, call the code under test after the setup and assert its result.
These examples omit the store and use NullStore. BuildAsync is optional for that case; it is shown at the final setup boundary so the same pattern works when TestBiome.Start receives a store.
The field keeps its anchor
An Add returns a new builder view over the same session. It does not move the anchor of a builder you already kept. Here, _baseline stays anchored to the constructor's class. Another _baseline.AddStudent(...) before the save adds a student to the same class and continues the seat sequence.
When a continuation needs an earlier parent, keep that entity in an instance field. For example, capture the school as _school and use .Under(_school) to add another class. Every branch belongs to the original session and completes in one BuildAsync. See Parents and children.
Construct synchronously, save after extending
Adds create entities immediately, so the constructor can prepare the baseline without asynchronous work. Constructors cannot await BuildAsync, and saving closes the session to further Adds. Keep the save in the test after its final Add; avoid blocking on asynchronous setup with .Result or .GetAwaiter().GetResult().
For EF Core, create a context for each test instance and pass it to TestBiome.Start. After the test extends the baseline, call BuildAsync once and clear the change tracker before the code under test reads. Dispose the context through the framework's per-test cleanup lifecycle. See EF Core store.
If creating the context or another dependency requires asynchronous initialization, use TUnit's per-test hooks or xUnit's IAsyncLifetime to prepare those resources. TUnit property injection happens after the constructor, so a constructor cannot use a property-injected context; start the baseline in a per-test hook once that dependency is available. Keep its Biome session open for the test's continuation.
Keep mutable state per test
Create the seed, builder, entities, mutable clock and context for each test instance. A static builder, a shared fixture containing a builder, or a shared fake clock would carry mutations between tests. Share immutable blueprints and frozen BiomeOptions; each constructor still calls Start to create a fresh session.
If the test advances time, create its provider with the baseline and pass the same instance to Biome and the code under test. See Time and precision.
Separate test instances isolate the in-memory graph. A shared database still needs test-specific rows and an isolation or cleanup strategy; a fresh constructor alone does not isolate database state.
Keep the constructor's baseline small: only rows every test in the class needs. Put scenario-specific values in the test's continuation, or use a named scenario helper when only some tests share a longer chain. That leaves the behavior under test visible at the call site.