Test It Where It’s Easy
A basic mantra: don’t test things where they’re hard to test.
A very basic application of this mantra comes up for almost every noob TDD’er: "How do I test this complex private method?" The answer is simple: make it not private somewhere else and test it there instead.
If your private is truly complex, it’s truly interesting, and needs testing. But ask why it has to be private? The most common answer: "I don’t want to expose it to users of MyCoolObject because applied wrong it could end life as we know it." Now, that’s an entirely legitimate answer. Nobody wants to be the one who exposed the API that ruined everything for everyone forever.
But remember that every class is a scope. You know this already or you wouldn’t be using ‘private’ at all. Private controls what’s visible. So. Make a new class. Give it a public method that is the complex private you had before. Make it in MyCoolObject’s constructor. Call it. Sure, it’s public over on OldPrivateMethod’s scope. But not on MyCoolObject’s, what u wanted by having it be private in the first place.
A complication arises, AND IT’S A GOOD ONE SO SUFFER THE PAIN THE FIRST FEW TIMES UNTIL YOU GET IT.
What if your complex private uses a whole bunch of fields from MyCoolObject? We enter judgment territory here. Q: does it use all the fields from MyCoolObject? Some, a couple, none?
If it’s a couple: pass them in. No biggie.
If it’s some, you’re very likely looking at one or more smells. Here are some of the possible smells you might find in this case.
Do u have temporary fields? Does MyCoolObject have too many responsibilities? Do the used (some) fields need aggregation to a new object?
If it’s all, then in fact you have a simple situation, tho it seems scary when u first see it: those fields belong on OldPrivateMethod…
After all, MyCoolObject can use them just as easily with them living on OldPrivateMethod as it does on itself.
The reason I mention the pain? Well, it goes back to the driving premise.
Remember, tests & testability are first-class citizens in design. When you’re a noob, this feels hard. It gets much easier, but you have to play through for a while to get the pieces put together correctly.
Anyway, hang in there, and think about that mantra: "If it’s hard to test here, why not test it somewhere else?"