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

How to get mscorlib methods in a static method of a static class

3 Answers 20 Views
General Discussions
This is a migrated thread and some comments may be shown as answers.
Gustavo
Top achievements
Rank 1
Gustavo asked on 24 May 2013, 10:16 PM
Hi, how do I mock the examble below?

I want o mock the Datetime method that is called inside the class.

Also, how to mock mscorlib methods for the whole class and not for a particular method?

Thanks,
Gustavo
public static class MyStaticClass
{
     public static string MyStaticMethod()
     {
          return Datetime.Now.ToString() + "anotherRandomString";
     }
}

3 Answers, 1 is accepted

Sort by
0
Kaloyan
Telerik team
answered on 27 May 2013, 01:23 PM
Hi Gustavo,

Thank you for reaching Telerik support.

In the latest JustMock version (JustMock Q2 Internal Build 2013.2.522), you will find the Mock.Replace() method obsoleted. Further release notes could be found here.

Currently, to mock MsCorlib members you will simply need to arrange them as desired. For example, I created the following test which is mocking the DateTime.Now and testing the provided by you static function:
[TestMethod]
public void TestMethod1()
{
    Mock.Arrange(() => DateTime.Now).Returns(new DateTime(2000, 12, 12));
 
    Assert.AreEqual("12/12/2000 12:00:00 AManotherRandomString", MyStaticClass.MyStaticMethod());
}
Note that, Mock.Replace() is not being used and still the test is passing. This should allow you to arrange behavior for any MsCorlib member in any class/method under test.

I hope this helps. Contact us again if you need more help.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
0
Gustavo
Top achievements
Rank 1
answered on 27 May 2013, 07:46 PM
Hi Kaloyan,

We are still using version 2013.1.220 of JustMock, could you please provide a snippet that will work on this version for this scenario.

The way that I see on docs for Mscorlib method is:

 Mock.Replace(() => DateTime.Now).In<MsCorlibFixture>(x => x.ShouldAssertCustomValueForDateTime());

But this doesn't work for static classes.

Thanks,
Gustavo
0
Kaloyan
Telerik team
answered on 29 May 2013, 02:50 PM
Hi Gustavo,

It is unfortunate to say, but such functionality is missing in JustMock 2013.1.220.

A workaround is to declare MyStaticClass as:
public abstract class MyStaticClass
Then, you will be able to use:
Mock.Replace(() => DateTime.Now).In<MyStaticClass>();
However, I understand in most of the cases this won`t be applicable, so I encourage you to update to the latest JustMock version.
 
I hope this helps.

Regards,
Kaloyan
Telerik
Share what you think about JustTrace & JustMock with us, so we can become even better! You can use the built-in feedback tool inside JustTrace, our forums, or our JustTrace or JustMock portals.
Tags
General Discussions
Asked by
Gustavo
Top achievements
Rank 1
Answers by
Kaloyan
Telerik team
Gustavo
Top achievements
Rank 1
Share this question
or