chevron-thin-right chevron-thin-left brand cancel-circle search youtube-icon google-plus-icon linkedin-icon facebook-icon twitter-icon toolbox download check linkedin phone twitter-old google-plus facebook profile-male chat calendar profile-male
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 by XChen29 (2.7k 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.
answered by alex (17k points)
0 votes
Thank you very much! Very helpful!
answered by XChen29 (2.7k points)
...