How do I unit test methods in a method object?

I performed " Replace Method with Method Object ", refactoring described by Beck .

Now I have a class with the "run ()" method and a bunch of member functions that decompose the calculations into smaller units. How to check those member functions?

My first idea is that my unit tests are basically copies of the "run ()" method (with different initializations), but with statements between each call to the member functions to check the state of the calculation.

(I am using Python and the unittest module.)

class Train: def __init__(self, options, points): self._options = options self._points = points # other initializations def run(self): self._setup_mappings_dict() self._setup_train_and_estimation_sets() if self._options.estimate_method == 'per_class': self._setup_priors() self._estimate_all_mappings() self._save_mappings() def _estimate_all_mappings(): # implementation, calls to methods in this class #other method definitions 

I definitely have expectations about what states of member attributes should be before and after calls to various methods as part of the implementation of the run() method. Should I make statements about these "private" attributes? I do not know how else to remove these methods.

Another option is that I really should not test them.

+6
python unit-testing tdd refactoring
source share
2 answers

I will answer my question. After a bit of reading and thinking, I believe that I should not be unit testing these private methods. I should just check the open interface. If private methods that perform internal processing are important enough for independent testing and are not just coincidences of the current implementation, then this is probably a sign that they should be reorganized into a separate class.

+10
source share

I like your answer, but I do not agree.

The situation in which you will use this design pattern is a situation in which a rather complicated operation occurs. As a result, I would say that abel checks the individual components of such an operation.

Then you have the problem of dependence on other resources (which may or may not be true in this case).

You need to be able to use some form of ioc to insert some form of layout to isolate the class.

In addition, most mocking frameworks will provide you with accessories to access private members.

+6
source share

All Articles