How do you unit test private methods?

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

    You should not be testing the private methods of your code in the first place. You should be testing the 'public interface' or API, the public things of your classes. The API are all the public methods you expose to outside callers.

    The reason is that once you start testing the private methods and internals of your class you are coupling the implementation of your class (the private things) to your tests. This means that when you decide to change your implementation details you will also have to change your tests.

    You should for this reason avoid using InternalsVisibleToAtrribute.

    Here is a great talk by Ian Cooper which covers this subject: Ian Cooper: TDD, where did it all go wrong

提交回复
热议问题