How do you unit test private methods?

前端 未结 30 1830
无人及你
无人及你 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:27

    I use PrivateObject class. But as mentioned previously better to avoid testing private methods.

    Class target = new Class();
    PrivateObject obj = new PrivateObject(target);
    var retVal = obj.Invoke("PrivateMethod");
    Assert.AreEqual(retVal);
    

提交回复
热议问题