How to unit test private methods in .NET Core applications (C#)
The best practice solution:
Test private method by public method !
Because your private methods are only implementation detail. The existence and behavior of them is only justified by their usage in public methods. Once your public methods is tested, your private methods should be fully covered
When you define the class which have public and private methods, you must to consider the private method can UnitTest by public method UnitTest or not. If not, you should be refactor the class to ensure the private methods can be test in public methods UnitTest
How to unit test private methods Event If It Is Bad ?
Sometime, in the legacy code, the private methods are big and complicated. You do not want to recreate all necessary context to call the public method to verify the private methods
In this case, we can use the Reflection to test the private method. Please do not forget that This should be truly exceptional
Nhận xét
Đăng nhận xét