How do you unit test private methods?

前端 未结 30 1853
无人及你
无人及你 2020-11-22 06:44

I\'m building a class library that will have some public & private methods. I want to be able to unit test the private methods (mostly while developing, but also it coul

30条回答
  •  故里飘歌
    2020-11-22 07:46

    In the rare cases I have wanted to test private functions, I have usually modified them to be protected instead, and the I have written a subclass with a public wrapper function.

    The Class:

    ...
    
    protected void APrivateFunction()
    {
        ...
    }
    
    ...
    

    Subclass for testing:

    ...
    
    [Test]
    public void TestAPrivateFunction()
    {
        APrivateFunction();
        //or whatever testing code you want here
    }
    
    ...
    

提交回复
热议问题