Mock DateTime.Now

17 posts, 0 answers
  1. zach davis
    zach davis avatar
    9 posts
    Member since:
    Jan 2010

    Posted 17 Sep 2010 Link to this post

    Hey Everyone,

    I am trying to mock DateTime.Now and having some issues.  Below is my code
    [Test]
    public void datetime_test_justmock()
    {
        var YEAR_2K = new DateTime(2000, 1, 1);
        Mock.Arrange(() => DateTime.Now).Returns(YEAR_2K);
        var actual = DateTime.Now;
        Assert.AreEqual(YEAR_2K, actual);
    }

    Actual gets sets to the current DateTime not the one I arranged.

    Am I missing something.

    Thanks,

    -zd
  2. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 20 Sep 2010 Link to this post

    Hi zach,

    Thank you for submitting the issue. I have prepared a tiny sample with your test code and attached it with the ticket. Please check it out.

    In regard to your failing test, please also cross check the following:

    • As DateTIme is a mscorlib class,  there is a MockClassAttribute on top of the test class.
    • If you are using NUnit and TestDriven.Net , make sure that you have the latest RTM of TestDriven.Net.
    • Please, make sure that you don’t have other mocking installed that uses profiler  which can interfere with JustMock profiler in mocking concrete members. 

    Please do write back if you have further questions.


    Regards,
    the Telerik team
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
  3. DevCraft R3 2016 release webinar banner
  4. tv
    tv avatar
    2 posts
    Member since:
    Jan 2006

    Posted 05 Jan 2012 Link to this post

    Hello,

    I got the same issue here:

    [TestClass, MockClass]
    public class DateTimeTest
    {
        [TestMethod]
        public void GetTimeTest()
        {
            DateTime expectedTime = new DateTime(2011,1,1);
            Mock.Arrange(() => DateTime.Now).Returns(expectedTime);

            Time time = new Time();
            DateTime now = time.GetTime();
            
            Assert.IsTrue(now.Year == 2011);
            Assert.IsTrue(now.Month == 1);
            Assert.IsTrue(now.Day == 1);
        }

    }

    public class Time
    {
        public DateTime GetTime()
        {
            return DateTime.Now;
        }
    }

    Is there a way to get this working?
  5. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 10 Jan 2012 Link to this post

    Hi Zach,

    Thanks for reporting the problem. At present the only way is to apply the MockClassAttribute on the Time class itself, in order to address nested mscorlib members. Therefore, it will look like:

    [TestClass, MockClass]
     public class DateTimeTest
     {
         [TestMethod]
         public void GetTimeTest()
         {
             DateTime expectedTime = new DateTime(2011,1,1);
             Mock.Arrange(() => DateTime.Now).Returns(expectedTime);
      
            Time time = new Time();
             DateTime now = time.GetTime();
              
             Assert.IsTrue(now.Year == 2011);
             Assert.IsTrue(now.Month == 1);
             Assert.IsTrue(now.Day == 1);
         }
      
    }
     
    [MockClass]
    public class Time
     {
         public DateTime GetTime()
         {
             return DateTime.Now;
         }
     }

     
    However, a fix for it will be available in the Q1 release where you don’t have to use the attribute at all.

    Kind Regards,
    Mehfuz
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  6. tv
    tv avatar
    2 posts
    Member since:
    Jan 2006

    Posted 29 Mar 2012 Link to this post

    Hi,

    when will the feature be available? I'm using version 2012.1.229.0 and I still have to use the MockClass attribute.

    Regards,
    tv
  7. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 29 Mar 2012 Link to this post

    Hi Zach,

    Thanks again for contacting us. The feature will be available hopefully in Q2 2012 release. We are currently working on this and almost there. 

    We appreciate your understanding.


    Kind Regards
    Mehfuz
    the Telerik team
    Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
  8. Andreas
    Andreas  avatar
    5 posts
    Member since:
    Dec 2011

    Posted 19 Apr 2012 Link to this post

    Isn't just a static mockup required to achieve what you want?

    Mock.SetupStatic(typeof(DateTime));

    Before calling

    var YEAR_2K = new DateTime(2000, 1, 1);
    Mock.Arrange(() => DateTime.Now).Returns(YEAR_2K);


  9. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 23 Apr 2012 Link to this post

    Hi Andreas,

    Thanks again for bringing up the question.

    Mock.SetupStatic is useful if you are doing strict mocking using Behavior.Strict or want to fake the static constructor or by default you want your static calls to act as stub. But this is not required. You can always mock DateTime.Now or any other static method directly inside Mock.Arrange.

    In addition, since Datetime, File or FileInfo is in default set of mscorlib members you don’t need to initialize them either using Mock.Initialize. More examples on this can be found here:

    http://www.telerik.com/help/justmock/advanced-usage-mscorlib-mocking.html

    Kind Regards,
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  10. Andreas
    Andreas  avatar
    5 posts
    Member since:
    Dec 2011

    Posted 24 Apr 2012 Link to this post

    Hello Ricky, many thanks for your explanation!
  11. Jeroen
    Jeroen avatar
    1 posts
    Member since:
    May 2012

    Posted 16 May 2012 Link to this post

    I don't like the use of the MockClassAttribute in for example a business layer, like you explained with the "time" class. Do you have an idea when the 2012 Q2 release is ready? As far as I understand: With 2012 Q2 I don't need make a reference any more in my business layer to the JustMock assembly. Currently I'am consider what framework we are going to use in our organisation and this can be a "no go" for JustMock. Or are there otherways to Mock a DateTime.Now?
  12. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 18 May 2012 Link to this post

    Hi Zach,
    Thanks again for bringing up the question.

    Good news is that you don't need to add MockClassAttribute on your test class after Q2 build. We fixed it and now it is no longer required.

    Kind Regards
    Ricky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  13. Christiano
    Christiano avatar
    17 posts
    Member since:
    Dec 2008

    Posted 22 May 2012 Link to this post

    Hi There!

    I have downloaded the lastest version of JustMock (commercial) and tried to fake/mock DateTime.Now. It worked fine, but only when I call it straight in a class decorated by "MockClass" attribute. This is a serious problem to me, because when faking this call, I need to extend this it to CLR Infrastructure level. There are a few assemblies in my product wich dont have the source, and I cant decorate it's classes.

    Is there any workaround for this kind of situation?
    Thank you, and congratulations for such great product
  14. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 25 May 2012 Link to this post

    Hi Christiano,

    Thanks again for contacting us. 

    Unfortunately, the only way to mock nested DateTime.Now as of now is to decorate the target class. However, we have come with a new solution that will be released in Q2 2012. This will let you mock mscorlib members without the need of MockClassAttribute on the target

    Kind Regards
    RIcky
    the Telerik team

    Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

  15. Chris Rathermel
    Chris Rathermel avatar
    28 posts
    Member since:
    Dec 2006

    Posted 01 Aug 2012 Link to this post

    Ricky,

    Any chance we can get a code example of how this is done in the current release and put this thread to bed? 

    Thanks,

    Chris
  16. Thorsten
    Thorsten avatar
    1 posts
    Member since:
    Dec 2011

    Posted 02 Aug 2012 Link to this post

    Hi Chris,

    this should work:

    public class Time
    {
        public DateTime GetTime()
        {
            return DateTime.Now;
        }
    }

    Unit Test:

    [TestFixture]
    public class DateTimeTest
    {
        [Test]
        public void GetTime_Returns_Current_Date()
        {
            DateTime expectedTime = new DateTime(2011, 1, 1);

            Mock.Replace(() => DateTime.Now).In<Time>(x => x.GetTime());

            Mock.Arrange(() => DateTime.Now).Returns(expectedTime);
            Time time = new Time();
            DateTime now = time.GetTime();

            Assert.IsTrue(now.Year == 2011);
            Assert.IsTrue(now.Month == 1);
            Assert.IsTrue(now.Day == 1);
        }

    }

  17. Sathyamurthy
    Sathyamurthy avatar
    5 posts
    Member since:
    Jul 2012

    Posted 07 Aug 2012 Link to this post

    I have found an issue with mocking DateTime.Now(may be this is applicable for all mscorlib mocking).  Taking the example given above, assuming that GetTime takes an argument of reference type which is defined in another assembly, it's failing. I mean mocking is not happening. Rather, DateTime.Now is actually called. I'm not sure if I'm missing something. Consider the following code. Assuming that "User" class is defined in a different assembly, the mocking is failing.

    public class Time
    {
        public DateTime GetTime(User user)
        {
            return DateTime.Now;
        }
    }

    Unit Test:

    [TestFixture]
    public class DateTimeTest
    {
        [Test]
        public void GetTime_Returns_Current_Date()
        {
            DateTime expectedTime = new DateTime(2011, 1, 1);
            User user= new User();
            Mock.Replace(() => DateTime.Now).In<Time>(x => x.GetTime(user));

            Mock.Arrange(() => DateTime.Now).Returns(expectedTime);
            Time time = new Time();
            DateTime now = time.GetTime(user);

            Assert.IsTrue(now.Year == 2011);
            Assert.IsTrue(now.Month == 1);
            Assert.IsTrue(now.Day == 1);
        }

    } 
  18. Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 10 Aug 2012 Link to this post

    Hi Sathyamurthy,
    Thanks again for contacting us.

    An answer to this issue has been posted in the following forum post please check it out:
    http://www.telerik.com/community/forums/justmock/general-discussions/error-while-trying-to-run-justmock-sample.aspx

    Kind Regards
    Mehfuz

    Explore the entire Telerik portfolio by downloading Telerik DevCraft Ultimate.

Back to Top
DevCraft R3 2016 release webinar banner