Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mocking Computer System Date Time

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.

like image 317
Marty Cochrane Avatar asked Jun 19 '12 10:06

Marty Cochrane


People also ask

Can you mock a DateTime now?

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.

How do you mock a Date object?

Mock Date object js const RealDate = Date; beforeEach(() => { global. Date. now = jest. fn(() => new Date('2019-04-22T10:20:30Z').

How to mock new Date() in jasmine?

mockDate(new Date(startingTime)); }); afterEach(() => { jasmine. clock(). uninstall(); }); // ... }

How do you mock Time in jest?

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.


2 Answers

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));
like image 163
sloth Avatar answered Oct 12 '22 22:10

sloth


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();
like image 34
Mharlin Avatar answered Oct 12 '22 22:10

Mharlin