David K. wrote in post #978200:
On Thu, Jan 27, 2011 at 8:39 PM, Marnen Laibow-Koser
Then you’ve got bigger problems. You shouldn’t be testing your private
methods; that’s poking too deeply into an object’s implementation. You
should only ever test things that can be called from outside an object.
If a private method is an intermediate value in a computation, just test
the end result. If a private method really needs to be tested
separately, then it’s telling you that it wants to be public.
I disagree – for me public is what I want to expose to outside access,
And therefore it is the only thing that is worth testing. No one cares
what your private logic is like, because they never see it.
regardless of complexity or size, where the private logic may actually
the most complex, being called by a relatively light public function.
Then test the return from the “light” public function, or make your
private logic public.
this may be a difference between TDD and BDD in pure forms, but for me I
find if I drive my methods, public or private with test first, that the
resulting code is much more pliable.
No private method should have a test at all. That’s testing
implementation, and therefore is bad. You want to test interface only.
Also, if I have coverage on what
becomes complex private logic, at a more granular level – then I have a
much stronger project an also address minute issues closer to the
No! All you have is brittle, implementation-dependent tests. What you
think is improving the quality of your code is actually hindering it.
am sure you have your method of working that works for you, this is what
have found effective and successful.
But it is neither. This is a matter of principles, not taste. Please
do not handwave away what I am telling you by saying that it is a matter
of taste – these are principles you need to understand. If you want to
disagree after you understand the principles, I will be interested to
hear your reasoning.
It is ineffective to test private methods because you never care about
the return value from a private method as such – if you cared about the
return value, you’d have made the method public.
It is unsuccessful to test private methods because you are tying your
tests too closely to implementation, which means they will fail if you
change your implementation – this despite the fact that one of the
points of testing is to tell you that your code still works when you
refactor your implementation.