Skip to content

Provides a test friendly wrapper over DateTime static properties

License

Notifications You must be signed in to change notification settings

tocsoft/DateTimeAbstractions

Repository files navigation

Tocsoft.DateTimeAbstractions

Tocsoft.DateTimeAbstractions is a testable alternative to the static DateTime.Now and DateTimeOffset.Now methods.

.NET codecov GitHub license

GitHub issues GitHub stars GitHub forks

Installation

We also have a MyGet package repository - for bleeding-edge / development NuGet releases.

Usage

Using Tocsoft.DateTimeAbstractions is as simple as switching out all usages of DateTime.Now with Clock.Now and your done your businesslogic thats dependent of on getting the current time is now more testable.

Using Tocsoft.DateTimeAbstractions from Tests

Now your code is using Clock.Now you can now wrap your code under test like so

using (Clock.Pin(new DateTime(2018, 02, 02))) 
{  
	// Clock.Now will always return the pinned value until the using block has ended.
	Assert.Equal(new DateTime(2018, 02, 02), Clock.Now);
}

Additional APIs

You probably aren't using DateTime.Now in you code so how can this library help? well we support all the static properties from DateTime and DateTimeOffset so any of this properties can be switched out and tested.

System API DateTimeAbstractions Replacement
DateTime.Now Clock.Now
DateTime.UtcNow Clock.UtcNow
DateTime.Today Clock.Today
DateTimeOffset.Now ClockOffset.Now
DateTimeOffset.UtcNow ClockOffset.UtcNow

We include warnings and code fixes for all these mappings.

In addition to the DateTime set of APIs we also include a testable wrapper around using the Stopwatch class in the form of the ClockTimer as with the Clock APIs we support pinning for testing purposes.

The Team

Lead

About

Provides a test friendly wrapper over DateTime static properties

Resources

License

Stars

Watchers

Forks

Packages

No packages published