I have built a Windows Service in C# that calls live timeseries data from a database based on the current date and time.
I have test data in a test Databse that I would like to use to calculate if the data was being used in the correct way.
I was wondering if anyone has used an application or has any other way to "Mock" the system date on your local computer. So for example I can just run this application and set it to set my system date to a specified date.
Any advice would be great.
Use Typemock Isolator, it can fake DateTime. Now and won't require you to change the code under test. Use Moles, it can also fake DateTime.
Mock Date object js const RealDate = Date; beforeEach(() => { global. Date. now = jest. fn(() => new Date('2019-04-22T10:20:30Z').
mockDate(new Date(startingTime)); }); afterEach(() => { jasmine. clock(). uninstall(); }); // ... }
To use the new mock system, you need to pass the "modern" argument to the jest. useFakeTimers function. This system will allow you not only to mock timers as you already could but also to mock the system clock. This way, the test will be green, but will also be stable in time.
Just encapsulate your access to the DateTime.Now
property behind an interface.
public interface IDateTimeProvider
{
DateTime Now { get; }
}
In your code, use an implementation like this:
public class DateTimeProvider : IDateTimeProvider
{
public DateTime Now { get { return DateTime.Now; } }
}
And for your tests, you can just mock IDateTimeProvider
by creating a test class or by using a mocking framework.
If you use this interface with a technique like depency injection, it's easy to alter the behaviour of your service, even at runtime.
You could e.g. create an IDateTimeProvider
that is always some hours off:
public class AlwaysToLateDateTimeProvider : IDateTimeProvider
{
public DateTime Now { get { return DateTime.Now.AddHours(-2); } }
}
or create an implementation that reads the 'mocked' datetime from a file, a database, a pipe etc.
While testing, you would configure your service to use one of those implementations, and while running in live mode, just configure your dependency injection to use the ordinary implementation that returns the 'right' date time.
And there is of course TypeMock Isolator...
Isolate.WhenCalled(() => DateTime.Now).WillReturn(new DateTime(2008, 1, 1));
I've used this to be able to override now, for instance when running tests. In your actual implementation where you use DateTime.Now you use the new SystemTime.Now instead. In your tests you just set the Now to a different function that returns your chosen value.
public static class SystemTime
{
private static Func<DateTime> now = () => DateTime.Now;
public static Func<DateTime> Now
{
get { return now; }
set { now = value; }
}
}
Example usage in test:
SystemTime.Now = () => DateTime.Now.AddMinutes(20);
In the unit test teardown it's important to set it back with SystemTime.Now = () => DateTime.Now
Normal usage:
DateTime now = SystemTime.Now();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With