Guides

Time and precision

One clock per test, frozen or shared with the code under test, rounded to match your database.

Shared context clock

Reading wall-clock time separately for each row can give related entities slightly different timestamps. That difference can obscure what the test intended. Biome gives each session one clock, accessed in blueprints through ctx.Now, ctx.Ago(…), and ctx.FromNow(…).

You pass…ctx.Now is…
no clockFrozen when Start is called. Every entity in the test sees the same Now.
a TimeProviderRead live. Advance a FakeTimeProvider during the test.

Suppose a class accepts enrollments for five days after it opens. Add an EnrollmentClosesAtUtc property to SchoolClass and set it with ctx.FromNow(TimeSpan.FromDays(5)) in the class blueprint. That gives every enrollment test the same deadline rule. The app's EnrollmentPolicy.IsOpen compares the deadline with the current time.

An expiry test is easier to reason about when setup and the code under test read the same clock. Pass one FakeTimeProvider to both, then advance it to exercise the transition.
// Give the setup and enrollment policy the same adjustable clock.
var time = new FakeTimeProvider(new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero));
await TestBiome
    .Start(out _, time: time)
    .AddDistrict(out _)
    .AddSchool(out _)
    .AddClass(out var algebra)
    .BuildAsync();

// Advance beyond the five-day enrollment window to exercise the cutoff.
await Assert.That(EnrollmentPolicy.IsOpen(algebra, time)).IsTrue();
time.Advance(TimeSpan.FromDays(6));
await Assert.That(EnrollmentPolicy.IsOpen(algebra, time)).IsFalse();

Frozen by default

Without a provider, Biome captures TimeProvider.System.GetUtcNow() once at Start. Entities built a moment apart still get identical timestamps.

DistrictTests.cs
var biome = TestBiome.Start(out _);

biome.AddDistrict(out var first);
await Task.Delay(20); // Wall time advances while the session clock stays frozen.
await biome.AddDistrict(out var second).BuildAsync();

await Assert.That(second.CreatedAtUtc).IsEqualTo(first.CreatedAtUtc);

A live clock between Adds

Adds are eager, so a test can advance the provider between Add calls before the single save.

StaffTests.cs
var instant = new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero);
var time = new FakeTimeProvider(instant);
var schoolStep = TestBiome.Start(out _, time: time).AddDistrict(out _).AddSchool(out _);
schoolStep.AddTeacher(out var first);

// Advance between Adds because each teacher takes its timestamp when built.
time.Advance(TimeSpan.FromHours(2));
await schoolStep.AddTeacher(out var later).BuildAsync();

await Assert.That(first.CreatedAtUtc).IsEqualTo(instant);
await Assert.That(later.CreatedAtUtc).IsEqualTo(instant.AddHours(2));

Relative times

Use the two-parameter form with ctx.Ago or ctx.FromNow. It works with any clock.

Relative time
await TestBiome
    .Start(out _)
    .AddDistrict(out _)
    .AddSchool(out _)
    .AddClass(out _)
    .AddOne(
        Blueprints.Student,
        out var returningStudent,
        // Backdate from the same clock the test uses to check enrollment age.
        (s, ctx) => s.EnrolledAtUtc = ctx.Ago(TimeSpan.FromDays(14))
    )
    .BuildAsync();

Precision rounding

A timestamp can be correct in memory and still change on a database round trip. Biome rounds clock readings to 1 µs by default to match Postgres timestamptz, so values built from those readings retain their precision when saved.

.NET stores 100 ns ticks. Postgres stores microseconds. An unrounded DateTimeOffset.UtcNow loses its last digit on a round trip, and an equality assertion can fail. Biome truncates every reading of "now" (frozen and test-supplied) before Ago and FromNow derive from it.

DistrictTimeTests.cs
// Include seven extra ticks beyond the last whole microsecond.
var instant = new DateTimeOffset(2026, 10, 11, 12, 0, 0, TimeSpan.Zero).AddTicks(1_234_567);
var time = new FakeTimeProvider(instant);

await using var db = postgres.CreateContext();
await TestBiome.Start(db, out _, time).AddDistrict(out var district).BuildAsync();

await using var read = postgres.CreateContext();
var saved = await read.Districts.SingleAsync(d => d.Id == district.Id);

// Biome drops the seven ticks before saving, so Postgres preserves the value.
await Assert.That(district.CreatedAtUtc).IsEqualTo(instant.AddTicks(-7));
await Assert.That(saved.CreatedAtUtc).IsEqualTo(district.CreatedAtUtc);
OptionEffect
default1 µs (TimeSpan.FromTicks(10)).
options.TimePrecision(TimeSpan.FromMilliseconds(1))Round to a millisecond.
options.TimePrecision(TimeSpan.Zero)Turn rounding off.
If timestamps differ by ticks or microseconds after a read, check whether setup used a separate wall-clock reading. Use the session clock or the shared FakeTimeProvider for comparisons, so precision and elapsed time do not introduce an unrelated difference.