Recently, I was working on the unit test case of legacy code, while writing test cases, I am come up with a problem.
Since I am writing unit test case not integration test. The problem is with private method. In our code a public method is dependent on 5 private method.
For unit test case, I required to give fake implementation of private method, but since method is private, I can't able to-do so. Because MOQ mocking framework did not support the mocking functionality of private method. and even I can't able to write test case of private method.
Possible approach I was thinking
Move all private method in different class. and make them public and also create an interface of this class. In this way I can able to give fake implementation at run-time because now we have Interface of the class.
But the problem in this approach is I need to make this private method to public just for unit test case. so it is a good practice to make it public.
I do commonly split classes when I start seeing myself writing a lot of private methods. But you have a couple of options:
Fully test the public interface only. You should cover your private methods as a matter of course.
You can make your private methods internal
and specify your test project as a friend assembly. Then they can be unit tested directly.
The solution you already thought of to just split up the class. Sometimes code being difficult to test indicates that there's a class inside trying to break out.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With