Enhanced.Testing.Component.Time
1.4.0
See the version list below for details.
dotnet add package Enhanced.Testing.Component.Time --version 1.4.0
NuGet\Install-Package Enhanced.Testing.Component.Time -Version 1.4.0
<PackageReference Include="Enhanced.Testing.Component.Time" Version="1.4.0" />
<PackageVersion Include="Enhanced.Testing.Component.Time" Version="1.4.0" />
<PackageReference Include="Enhanced.Testing.Component.Time" />
paket add Enhanced.Testing.Component.Time --version 1.4.0
#r "nuget: Enhanced.Testing.Component.Time, 1.4.0"
#:package Enhanced.Testing.Component.Time@1.4.0
#addin nuget:?package=Enhanced.Testing.Component.Time&version=1.4.0
#tool nuget:?package=Enhanced.Testing.Component.Time&version=1.4.0
Enhanced.Testing.Component.Time
A time harness for Enhanced.Testing.Component: replaces the
application's TimeProvider with a FakeTimeProvider the test controls, so delays, timeouts, retries and timers
complete as soon as the test moves the clock instead of waiting for real time. No Docker needed.
dotnet add package Enhanced.Testing.Component.Time
Usage
var time = new TimeProviderHarness(new DateTimeOffset(2030, 1, 1, 0, 0, 0, TimeSpan.Zero));
var component = ComponentBuilder.Create<Program>()
.AddHarness(time)
.AddHarness(http)
.Build();
In a test, move the clock instead of waiting:
await client.PostAsJsonAsync("/subscriptions", new { Plan = "trial" });
time.Advance(TimeSpan.FromDays(15));
var subscription = await client.GetFromJsonAsync<Subscription>("/subscriptions/current");
Assert.True(subscription!.Expired);
When code is already waiting on the fake clock (for example, a Task.Delay(..., timeProvider) in a request that is
still running), Advance completes the wait immediately. Advance only after the code has started waiting, or keep
advancing until the request finishes.
Advance(TimeSpan)andSetUtcNow(DateTimeOffset)move the clock forward;UtcNowreads it.TimeProvideris theFakeTimeProvideritself, for anything else it supports (for example,AutoAdvanceAmount).new TimeProviderHarness()starts at theFakeTimeProviderdefault of 2000-01-01 UTC. Pass a start time if the application checks dates such as certificate or token expiry.
What it affects
The harness replaces every TimeProvider registration in the application's service provider. It affects only code
that resolves TimeProvider from there, including framework and library services that do so (for example,
resilience handlers): their timeouts and retry delays no longer elapse on their own, only when the test moves the
clock. DateTime.UtcNow, TimeProvider.System and the containers keep the real clock.
Register TimeProvider in the application and inject it where you need time:
builder.Services.AddSingleton(TimeProvider.System);
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 is compatible. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net11.0 is compatible. |
-
net10.0
- Enhanced.Testing.Component (>= 1.4.0)
- Microsoft.Extensions.TimeProvider.Testing (>= 11.0.0)
-
net11.0
- Enhanced.Testing.Component (>= 1.4.0)
- Microsoft.Extensions.TimeProvider.Testing (>= 11.0.0)
-
net8.0
- Enhanced.Testing.Component (>= 1.4.0)
- Microsoft.Extensions.TimeProvider.Testing (>= 11.0.0)
-
net9.0
- Enhanced.Testing.Component (>= 1.4.0)
- Microsoft.Extensions.TimeProvider.Testing (>= 11.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.