0

i'm starting to build a part of a system which will hold a lot of DateTime validations, and a lot of 'if it was done before now' or 'if it will start in an hour etc'.

Usual way to go is to use DateTime.Now to get the actual time.

I predict however, that during unit test that will give me a real headache because i will have to setup my testdata for the time when the test will run in stead of use a default set of test data. So i thought: why not use my own 'now' so i can set the current datetime to any moment in time. As i don't want to set the testservers internal clock i was thinking about this solution, and i was wondering what you think of it. Base thought is that i use my own DateTime class. That class gives you the current datetime, but you can also set your own time from outside.

 public static class MyDateTime
    {
        private static TimeSpan _TimeDifference = TimeSpan.Zero;
        public static DateTime Now
        {
            get
            {
                return DateTime.Now + _TimeDifference;
            }
        }
        public static void SetNewNow(DateTime newNow)
        {
            _TimeDifference = newNow - DateTime.Now;
        }
        public static void AddToRealTime(TimeSpan timeSpan )
        {
            _TimeDifference =  timeSpan;
        }
        public static void SubtractFromRealTime(TimeSpan timeSpan)
        {
            _TimeDifference =  - timeSpan;
        }
    }
Michel
  • 23,085
  • 46
  • 152
  • 242
  • what's wrong with the Framework's DateTime built-in methods? – Mitch Wheat Jun 02 '10 at 09:07
  • Possible duplicate: http://stackoverflow.com/questions/2425721/unit-testing-datetime-now – Mark Seemann Jun 02 '10 at 09:11
  • I'd avoid calling it `MyDateTime`, too easy to mistake for `DateTime`, better to call it something easier to distinguish. – Hans Olsson Jun 02 '10 at 09:17
  • @Mitch i wouldn't know how to reset the datetime.now ? – Michel Jun 02 '10 at 09:41
  • @mark i agree, but most answers go directly to a framework. we're not using a mock framework right now, and i can't introduce an entire mocking framework (even if i use just a part of it) just for this unit tests. – Michel Jun 02 '10 at 09:42
  • @Michel: Who said anything about a mocking framework? The answer provided to the other question defaults to using the static DateTime property. – Mark Seemann Jun 02 '10 at 10:22

2 Answers2

3

Some time ago i already read something about it. A short search for mock DateTime.Now with your favorite search engine should reveal enough links.

This seems to look quite interesting: http://blog.typemock.com/2009/05/mockingfaking-datetimenow-in-unit-tests.html

Oliver
  • 43,366
  • 8
  • 94
  • 151
  • isn't that a paid product? besides that, i'm a little resistant to implement/use a whole new framework of which i don't know anything for this small functionality. – Michel Jun 02 '10 at 09:40
0

what do u mean ? if u need any specialization, so u can use Extension methods ! easily write an extension method for DateTime class and do whatever u need.

dr_csharp
  • 63
  • 3