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:
- Production code accepts an optional TimeProvider parameter (defaults to System).
- Tests construct a Microsoft.Extensions.Time.Testing.FakeTimeProvider and inject it.
- Tests call
fakeTime.Advance(TimeSpan)orfakeTime.SetUtcNow(...)to drive time forward deterministically. - 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
Returns
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
valueFakeTimeProviderThe fake time provider.
totalTimeSpanExpected total elapsed since construction.
Returns
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
valueFakeTimeProviderexpectedDateTimeOffset
Returns
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
valueFakeTimeProviderexpectedDateTimeOffsettoleranceTimeSpan