When I find myself wanting to test the private functions of a class that has a small public API and a complex internal call structure I seem to end up choosing from the two following approaches:
- If the class has functionality that is not reliant on the class' state and would offer useful functionality to other potential client code then I should break it out into a service and test it's public API.
- If the class has functionality that is reliant on class' state and would be tightly coupled if broken out then I should test it through the public API by passing the correct parameters and then name the test so that it references the private function I am targeting.
I feel that testing private functions directly makes classes less easy to refactor and tests more brittle but testing private functions through the public API and binding them just by name and correct parameter values also feels a bit shoddy.
Is there a set of rules to abide by in these situations short of doing proper TDD? I have no choice as I am writing tests in retrospect.