The answer to "Should I test private methods?" is ".......sometimes". Typically you should be testing against the interface of your classes.
- One of the reasons is because you do not need double coverage for a feature.
- Another reason is that if you change private methods, you will have to update each test for them, even if the interface of your object hasn't changed at all.
Here is an example:
class Thing
def some_string
one + two
end
private
def one
'aaaa'
end
def two
'bbbb'
end
end
class RefactoredThing
def some_string
one + one_a + two + two_b
end
private
def one
'aa'
end
def one_a
'aa'
end
def two
'bb'
end
def two_b
'bb'
end
end
In RefactoredThing
you now have 5 tests, 2 of which you had to update for refactoring, but your object's functionality really hasn't changed. So let's say that things are more complex than that and you have some method that defines the order of the output such as:
def some_string_positioner
if some case
elsif other case
elsif other case
elsif other case
else one more case
end
end
This shouldn't be run by an outside user, but your encapsulating class may be to heavy to run that much logic through it over and over again. In this case maybe you would rather extract this into a seperate class, give that class an interface and test against it.
And finally, let's say that your main object is super heavy, and the method is quite small and you really need to ensure that the output is correct. You are thinking, "I have to test this private method!". Have you that maybe you can make your object lighter by passing in some of the heavy work as an initialization parameter? Then you can pass something lighter in and test against that.