This is a migrated thread and some comments may be shown as answers.

Mock DateTime.Now

16 Answers 531 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
zach davis
Top achievements
Rank 1
zach davis asked on 17 Sep 2010, 07:00 PM
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

16 Answers, 1 is accepted

Sort by
0
Ricky
Telerik team
answered on 20 Sep 2010, 11:54 AM
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
0
tv
Top achievements
Rank 1
answered on 05 Jan 2012, 06:07 PM
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?
0
Ricky
Telerik team
answered on 10 Jan 2012, 09:28 AM
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 >>

0
tv
Top achievements
Rank 1
answered on 29 Mar 2012, 11:37 AM
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
0
Ricky
Telerik team
answered on 29 Mar 2012, 06:08 PM
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 >>
0
Andreas
Top achievements
Rank 1
answered on 19 Apr 2012, 12:22 PM
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);


0
Ricky
Telerik team
answered on 23 Apr 2012, 06:57 PM
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 >>

0
Andreas
Top achievements
Rank 1
answered on 24 Apr 2012, 07:49 AM
Hello Ricky, many thanks for your explanation!
0
Jeroen
Top achievements
Rank 1
answered on 16 May 2012, 06:19 PM
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?
0
Ricky
Telerik team
answered on 18 May 2012, 11:31 PM
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 >>

0
Christiano
Top achievements
Rank 2
answered on 22 May 2012, 05:45 AM
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
0
Ricky
Telerik team
answered on 25 May 2012, 03:49 PM
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 >>

0
Chris Rathermel
Top achievements
Rank 1
answered on 01 Aug 2012, 10:03 PM
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
0
Thorsten
Top achievements
Rank 1
answered on 02 Aug 2012, 06:39 AM
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);
    }

}

0
Sathyamurthy
Top achievements
Rank 1
answered on 07 Aug 2012, 07:21 AM
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);
    }

} 
0
Ricky
Telerik team
answered on 10 Aug 2012, 03:50 PM
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.

Tags
General Discussions
Asked by
zach davis
Top achievements
Rank 1
Answers by
Ricky
Telerik team
tv
Top achievements
Rank 1
Andreas
Top achievements
Rank 1
Jeroen
Top achievements
Rank 1
Christiano
Top achievements
Rank 2
Chris Rathermel
Top achievements
Rank 1
Thorsten
Top achievements
Rank 1
Sathyamurthy
Top achievements
Rank 1
Share this question
or