
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
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

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?
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 >>

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
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

Mock.SetupStatic(typeof(DateTime));
Before calling
var YEAR_2K = new DateTime(2000, 1, 1);
Mock.Arrange(() => DateTime.Now).Returns(YEAR_2K);
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 >>


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 >>

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
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 >>

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

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);
}
}

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);
}
}
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.