MsCorlib Mocking with a static class: is it possible?

5 posts, 2 answers
  1. DenisCL
    DenisCL avatar
    69 posts
    Member since:
    Dec 2009

    Posted 16 Aug 2010 Link to this post

    Hi,

    I've been trying to mock calls the the System.Environment class in MsCorlib.

    I followed the guidelines in the documentation:
    • I created the class with the MockClass attribute:
      [TestClass, MockClass]
      public class UnitTest1
    • I tried to use the partial method:
      Mock.Partial<Environment>().For<string>((x) => Environment.GetFolderPath(Arg.IsAny<Environment.SpecialFolder>()));
    But this doesn't feel right (the lambda uses an instance but the class is static) and it does not compile for the same reason: cannot use static types as type arguments.

    Is there a way to mock static mscorlib classes? (Environment.GetFolderPath)

    Thanks!
    DenisCL
  2. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 16 Aug 2010 Link to this post

    Hi DenisCL,

    Thanks for asking the question. As Environment is a static type, you can't use it directly as a generic argument. The compiler simply doesn't allow that. Also, looking at your test, I would like to inform you that Arg.IsAny does not have any effect at Mock.Partial that only setups the interceptor for mocking the specified framework member.

    As an example let me rewrite the test, just to give a head start.

    Firstly, in test initialization  you will partially setup the target member for mocking in the following way:

    [ClassInitialize] 
     public static void Initialize(TestContext context) 
     
         // static mscorlib members other than File, DateTime 
         Mock.Partial(typeof(Environment)) 
             .For<Environment.SpecialFolder, string>((i, j) => Environment.GetFolderPath(i)); 
     }


    Then, the rest is similar to mocking any other member, where you add your criteria for that member using helpers like Arg.IsAny<T>():

    [TestMethod]
    public void ShouldMockMembersFromEnviroment()
    {
        string expected = "ping";
        Mock.Arrange(() => Environment.GetFolderPath(Arg.IsAny<Environment.SpecialFolder>())).Returns(expected);
        Assert.Equal(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), expected);
    }


    Hope the information is useful.

    Best wishes,
    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. DenisCL
    DenisCL avatar
    69 posts
    Member since:
    Dec 2009

    Posted 17 Aug 2010 Link to this post

    Hi,

    Thanks for the answer!

    However, I am having trouble using the Partial method the way you pointed out.
    In my version (2010.1.713.7 - le last one I can get my hands on), I cannot find an overload of Partial that takes a Type as a parameter.
    Only the generic one is available: 
    public static MockInitializer<T> Partial<T>()


    Is this a feature that will be available in the next version?

    Thanks,
    DenisCL
  5. Answer
    Ricky
    Admin
    Ricky avatar
    467 posts

    Posted 17 Aug 2010 Link to this post

    Hi DenisCL,

    Thanks again for replying. The feature is added in SP1 release and the version number is 2010.2 810. I would request you to download the lastest build that is available through your telerik account.




    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
  6. DenisCL
    DenisCL avatar
    69 posts
    Member since:
    Dec 2009

    Posted 17 Aug 2010 Link to this post

    You guys are releasing updates quite often, that's great.

    It solves my problem,
    Thank you!

    DenisCL
Back to Top
DevCraft R3 2016 release webinar banner