Welcome to Typemock Answers. Here you can ask and receive answers from other community members. And if you liked or disliked an answer or thread: react with an up- or downvote Enjoy!

How can I mock some variables in the private static method?

0 votes
I want to test the behavior of a private static method of a public class.
I need to mock the parameter NameDirectory c, and the behavior of NameSystem.Find(c); I went through the examples, but i did not find a way to do that. Would anyone share some thoughts on that?

public class Foo{

     private static Location Parse(int a, NameDirectory c)
    {
           NameSystem.Find(c);
           ...
            return new Location(a);
    }

}
asked Dec 3, 2012 by XChen29 (320 points)

2 Answers

0 votes
Hi,

This example should do the trick of handling private static method:

 public class Foo
{
    private static int Parse(int a, string c)
    {
        return 3;
    }
}

[TestClass]
public class UnitTest1
{
    [TestMethod]
    public void TestMethod1()
    {
       //ARRANGE - faking private static method
       Isolate.NonPublic.WhenCalled(typeof (Foo) , "Parse").WillReturn(5);

       //ACT - invoking private static method
       var num =  Isolate.Invoke.Method(typeof (Foo), "Parse", new object[] {1, "something"});

       //use one of the ASSERTs below
       Assert.AreEqual(5 , num);

       //verifying private static method
       Isolate.Verify.NonPublic.WasCalled(typeof(Foo) , "Parse");
    }
}

You can find more info on this topic here.

Please let me know if it helps.

 

Alex Galin | Support Team

answered Dec 3, 2012 by alex (17,910 points)
0 votes
Thank you very much! Very helpful!
answered Dec 5, 2012 by XChen29 (320 points)
...