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 must have missed how to do this in the documentation...

How do I mock a private static method?

Thanks!
asked by sebastianpatten (2.2k points)

2 Answers

0 votes
Hi
Just like you mock public instance method using reflective mocks :)

public class MyClass
{
    private static int StaticPrivateMethod()
    {
        return 1;
    }

    public static int StaticPublicMethod()
    {
        return StaticPrivateMethod();
    }
}

[TestFixture]
public class TestClass
{
    [Test]
    public void Test()
    {
        Mock mock = MockManager.Mock<MyClass>();
        mock.ExpectAndReturn("StaticPrivateMethod", 5);

        Assert.AreEqual(5, MyClass.StaticPublicMethod());
    }         
}

See the documentation here
for details how to mock private methods using MSTest and natural mocks.
answered by ohad (35.4k points)
0 votes
Thanks
answered by sebastianpatten (2.2k points)
...