Provides testable and mockable clock implementations.
First, set the clock provider in your application:
static void Main(string[] args)
{
Clock.Provider = new SystemClock();
...
}
Next, use Clock.UtcNow
to get the time in the same way as you'd use DateTimeOffset.UtcNow
or DateTime.UtcNow
:
string GetTimestampString()
{
return Clock.UtcNow.ToString("yyyy'-'MM'-'dd'T'HH':'mm':'ss")
}
Now, you can change the clock provider in your test methods:
[Test]
void GetTimestampString_ReturnsCorrectFormattedTimestamp()
{
// Arrange
Clock.Provider = new ConstantClock(new DateTimeOffset(2017, 10, 29, 08, 39, 00, TimeSpan.Zero));
// Act
var timestamp = GetTimestampString();
// Assert
timestamp.Should().Be("2017-10-29T08:39:00");
}
PM> Install-Package NClock
Copyright Matthew King 2016-2019.
NClock is licensed under the MIT License. Refer to license.txt for more information.