Skip to content
Simple library for abstracting time in dotnet projects
C#
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
samples/SimpleWebSample
src
tests/Chronos.Abstractions.UnitTests
.gitignore
Chronos.Net.sln
LICENSE
README.md
azure-pipelines.yml
icon.png

README.md

NuGet Version Build Status Quality Gate Status

Chronos.Net

Simple library to help abstracting time in dotnet projects, usually for testing purpose.

Chronos.Abstractions

NuGet Version Chronos.Abstractions package in Vivien_NuGet feed in Azure Artifacts

Contains:

  • An IDateTimeProvider interface which exposes Now or UtcNow properties.

Chronos.Net

NuGet Version Chronos.Net package in Vivien_NuGet feed in Azure Artifacts

Contains:

  • A simple implementation DateTimeProvider of the IDateTimeProvider interface.

Chronos.AspNetCore

NuGet Version Chronos.AspNetCore package in Vivien_NuGet feed in Azure Artifacts

Contains:

  • A WebHostBuilder extension to provide the default implementation DateTimeProvider for the IDateTimeProvider interface, by simply using UseDateTimeProvider()
public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
    .UseDateTimeProvider()
    .UseStartup<Startup>();

Then instead of using DateTime.Now or DateTime.UtcNow, you should use IDateTimeProvider.Now or IDateTimeProvider.UtcNow, which would enable you to fake/mock the time in your tests.
Example with FakeItEasy:

var dateTimeProvider = A.Fake<IDateTimeProvider>();
A.CallTo(() => dateTimeProvider.Now).Returns(new DateTime(2019, 3, 6, 9, 0, 0));

A sample web project can be found in the samples folder

You can’t perform that action at this time.