Before anyone asks ...
The solution to the OP problem is to properly enable IoC with DI and eliminate the need to test a private method in general (as Joel Martinez noted ). As mentioned several times , unit testing of private members is not the way to go.
However , sometimes you just can’t change the code (outdated systems, the risk of hacking changes - you name it), and also can’t use tools that allow you to test private members (for example, Typemock, which is a paid product). For such cases, you can either not test at all or cut corners. Which I consider the situation of OP.
Closing discussion of closed testing methods ...
Remember that you can use reflection to access and call private members.
In my opinion, placing conditional debugs in a class is a pretty bad idea - it adds noise (like something unrelated) to the class code. Of course, this will be released, but you (and possibly other programmers) will have to deal with this on a day to day basis.
I understand that your idea may seem good on paper - a simple test wrapped in conditional debugging. But in fact, tests quickly use additional variables (they should also be placed in the class code), some utilities (additional links, custom types), the testing framework (even more links), and what not. All this must be somehow related to the class code. Put it all together and you will quickly get an invisible monster.
Are you sure you want to handle this? Especially considering that merging a simple reflection-based utility may not be that difficult.
source share