I have a class which has one method that is called from another class. This method internally calls several other methods to do its work. Those other methods are all public and can be called by the other class but I don't use them that way because I'm trying to follow the "Tell-Don't-Ask" principle. All these methods return something back and they don't mutate the state of the input parameters. They do all their processing logic using only the input parameters passed in.
Now when it comes to unit testing, I can do these things but I'm not sure which one to do
- Test all methods by passing in input and asserting the actual output against an expected one.
- Test just the internal "public" methods (instead of all methods) like I described above and also test that the method that is called by the other class invokes the internal methods with the correct parameters i.e. use a mocking framework to mock out those calls but still assert that the calls are made.
This is an example. The enhanceThingInformation
method is the one that is called by the other class. The other methods are pretty specific to this class so I don't thing breaking them out to their own class is the right approach here (though it might be?).
class ThingInformationEnhancer {
addErrorInformation(thing, someDictionary1, someDictionary2) {
thing = /* some logic here */
/* more logic here */
return thing;
}
removeInvalidInformation(thing, someDictionary2) {
thing = /* some logic here */
/* more logic here */
return thing;
}
addAdditionalInformation(thing, someDictionary1, someDictionary2) {
thing = /* some logic here */
/* more logic here */
return thing
}
enhanceThingInformation(thing, someDictionary1, someDictionary2) {
thing = this.addErrorInformation(thing, someDictionary1, someDictionary2);
thing = this.removeInvalidInformation(thing, someDictionary2);
thing = this.addAdditionalInformation(thing, someDictionary1, someDictionary2);
return thing;
}
}
I'm interested to hear how others handle this sort of situation.