Tocsoft.DateTimeAbstractions is a testable alternative to the static DateTime.Now
and DateTimeOffset.Now
methods.
We also have a MyGet package repository - for bleeding-edge / development NuGet releases.
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.
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);
}
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.
Lead