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

4 posts, 0 answers
  1. Gustavo
    Gustavo avatar
    3 posts
    Member since:
    May 2013

    Posted 24 May 2013 Link to this post

    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";
         }
    }
  2. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 27 May 2013 Link to this post

    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.
  3. DevCraft R3 2016 release webinar banner
  4. Gustavo
    Gustavo avatar
    3 posts
    Member since:
    May 2013

    Posted 27 May 2013 Link to this post

    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
  5. Kaloyan
    Admin
    Kaloyan avatar
    872 posts

    Posted 29 May 2013 Link to this post

    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.
Back to Top