Hand-crafted fakes to make time-related testing easier. FakeTimeProvider implements the abstract TimeProvider class and is specifically designed to be used for testing.
$ dotnet add package Microsoft.Extensions.TimeProvider.TestingProvides a FakeTimeProvider for testing components that depend on System.TimeProvider.
From the command-line:
dotnet add package Microsoft.Extensions.TimeProvider.Testing
Or directly in the C# project file:
<ItemGroup>
<PackageReference Include="Microsoft.Extensions.TimeProvider.Testing" Version="[CURRENTVERSION]" />
</ItemGroup>
FakeTimeProvider can be used to manually adjust time to test time dependent components in a deterministic way.
FakeTimeProvider derives from TimeProvider and adds the following APIs:
public FakeTimeProvider(DateTimeOffset startDateTime)
public DateTimeOffset Start { get; }
public TimeSpan AutoAdvanceAmount { get; set; }
public void SetUtcNow(DateTimeOffset value)
public void Advance(TimeSpan delta)
public void SetLocalTimeZone(TimeZoneInfo localTimeZone)
These can be used as follows:
var timeProvider = new FakeTimeProvider();
var myComponent = new MyComponent(timeProvider);
timeProvider.Advance(TimeSpan.FromSeconds(5));
myComponent.CheckState();
We welcome feedback and contributions in our GitHub repo.