Table of Contents

Class FakeTimeProviderAssertions

Namespace
TimeAssertions.TUnit
Assembly
TimeAssertions.TUnit.dll

Fluent assertions on Microsoft.Extensions.Time.Testing.FakeTimeProvider instances. The headline use case for the family: tests that inject Microsoft.Extensions.Time.Testing.FakeTimeProvider into production code and then call Advance(TimeSpan) to simulate time passing can verify the resulting state with these assertions.

public static class FakeTimeProviderAssertions
Inheritance
FakeTimeProviderAssertions
Inherited Members

Remarks

The Microsoft-recommended pattern for testable time in modern .NET:

  1. Production code accepts an optional TimeProvider parameter (defaults to System).
  2. Tests construct a Microsoft.Extensions.Time.Testing.FakeTimeProvider and inject it.
  3. Tests call fakeTime.Advance(TimeSpan) or fakeTime.SetUtcNow(...) to drive time forward deterministically.
  4. Tests assert that production code reacted correctly: including the Microsoft.Extensions.Time.Testing.FakeTimeProvider's own state.

Methods

HasAdvancedApproximately(FakeTimeProvider, TimeSpan, TimeSpan)

Asserts that the Microsoft.Extensions.Time.Testing.FakeTimeProvider's current time differs from its construction-time start by approximately total, within tolerance. Use this when production code performs additional internal Advance calls that you want to allow for without exact matching.

[GenerateAssertion(ExpectationMessage = "to have advanced by approximately {total} within tolerance {tolerance}", InlineMethodBody = true)]
public static bool HasAdvancedApproximately(this FakeTimeProvider value, TimeSpan total, TimeSpan tolerance)

Parameters

value FakeTimeProvider
total TimeSpan
tolerance TimeSpan

Returns

bool

HasAdvancedExactly(FakeTimeProvider, TimeSpan)

Asserts that the Microsoft.Extensions.Time.Testing.FakeTimeProvider's current time differs from its construction-time start by exactly the specified total. Useful as a sanity check that Advance calls in test setup landed correctly.

[GenerateAssertion(ExpectationMessage = "to have advanced by total {total}", InlineMethodBody = true)]
public static bool HasAdvancedExactly(this FakeTimeProvider value, TimeSpan total)

Parameters

value FakeTimeProvider

The fake time provider.

total TimeSpan

Expected total elapsed since construction.

Returns

bool

HasUtcNow(FakeTimeProvider, DateTimeOffset)

Asserts that fakeTime.GetUtcNow() equals expected exactly. Useful for tests that snap the fake clock to a specific moment via SetUtcNow(DateTimeOffset) and want to confirm the snap.

[GenerateAssertion(ExpectationMessage = "to have UTC now {expected}", InlineMethodBody = true)]
public static bool HasUtcNow(this FakeTimeProvider value, DateTimeOffset expected)

Parameters

value FakeTimeProvider
expected DateTimeOffset

Returns

bool

HasUtcNowApproximately(FakeTimeProvider, DateTimeOffset, TimeSpan)

Asserts that fakeTime.GetUtcNow() is within tolerance of expected. Useful when the expected moment is computed (e.g. from integer-truncated minute math or chained Advance calls with rounding) rather than a literal: avoids exact-match brittleness.

[GenerateAssertion(ExpectationMessage = "to have UTC now approximately {expected} within tolerance {tolerance}", InlineMethodBody = true)]
public static bool HasUtcNowApproximately(this FakeTimeProvider value, DateTimeOffset expected, TimeSpan tolerance)

Parameters

value FakeTimeProvider
expected DateTimeOffset
tolerance TimeSpan

Returns

bool