chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
0 votes
I know that you can mock DateTime.Now but should it be possible to Isolate on DateTime.Today?

Thank you for your time.
asked by dblack (8.4k points)

6 Answers

0 votes
Hi Dave,

Thank you for bringing this up.
This is now in our backlog and will be added in the future.
answered by alex (17k points)
0 votes
Hi Alex,

Thanks for the reply. Do you have to implement each property seperately? Can't you just implement the ability to Isolate on the entire DateTime structure instead of one property at a time?

Thanks for your time.
answered by dblack (8.4k points)
0 votes
Hi Dave,

Sorry for the late reply.
While it's not a complete method-by-method implementation, it's more of checking side-effects.
When we started out, DateTime.Now was created manually, and that's why Today wasn't. When we do new mscorlib refresh, we'll do it the new way, which is more automatic.

Gil
answered by gilz (14.5k points)
0 votes
I also need DateTime.Today to be synchronized with DateTime.Now and DateTime.UtcNow

We have implemented an Aspect subclass in which we substituted our own Now and UtcNow that are synchronized. If the times I am substituting are from today then I have no issues, but if I return times from another day then Today is no longer synchronized as it remains the true 'today' of the local time.

I see from using ReSharper that the system implementation of DateTime.Today is simply { return DateTime.Now.Date; }. Is there a way to get it to call the get_Now in the Aspect class I created?
Failing that, do you anticipate the extension of your intercept class to be done in the next release?
Finally, is there a workaround or a way of substituting our own 'InterceptDateTime' class?

Thanks,
Peter Wood
ITG Software Solutions
USA
answered by PeterWood (180 points)
0 votes
I also need DateTime.Today to be synchronized with DateTime.Now and DateTime.UtcNow

We have implemented an Aspect subclass in which we substituted our own Now and UtcNow that are synchronized. If the times I am substituting are from today then I have no issues, but if I return times from another day then Today is no longer synchronized as it remains the true 'today' of the local time.

I see from using ReSharper that the system implementation of DateTime.Today is simply DateTime.Now.Date. Is there a way to get it to call the get_Now in the Aspect class I created?
Failing that, do you anticipate the extension of your intercept class to be done in the next release?

Finally, is there a workaround or a way of substituting our own 'InterceptDateTime' class?

Thanks,
Peter Wood
answered by PeterWood (180 points)
0 votes
Hi Peter & Dave,

You can find a patch that solves the DateTime.Today issue here

Please notice this example that demonstrate how use your own DateTime.Today:


    public class myDateTime
    {
        public DateTime myDateTimeNow()
        {
            return new DateTime(2000, 01, 01);
        }
    }



        [TestMethod]
        public void TestMethod()
        {
            var a = new myDateTime();
            Isolate.WhenCalled(() => DateTime.Now.Date).DoInstead((c) =>
                {
                    return a.myDateTimeNow();
                });
            
            var expectedVal = new DateTime(2000, 01, 01);
            Assert.AreEqual (expectedVal, DateTime.Today);
            
        }




Please let me know if it helps.
answered by NofarC (4k points)
...