Unit Testing a large method

automated-tests, tdd, unit-testing

Solution

If your steps are large enough (or are meaningful in their own right) you should consider delegating them to other smaller classes and test the interaction between your class and them. For example if you have a parsing step followed by a sorting step followed by a searching step it could be meaningful to have a parser class, a sorter class, etc. You would then use TDD on each of those.

No idea which language you're using, but if you're in the .net world you could make these classes internal and then expose them to your test class with "internals visible to" which would keep them hidden.

If the steps are small AND meaningless on their own then tvanfosson's suggestions are the way to go.

Problem

Following Test-Driven Development that is. I've recently implemented a algorithm (A*) that required a clean interface. By clean all I want is a couple of properties and a single search method. What I've found hard is testing the search method. It contains around five steps but I'm essentially forced to code this method in one big go which makes things hard. Is there any advice for this? Edit I'm using C#. No I don't have the code at hand at the moment. My problem relies in the fact a test only passes after implementing the whole search method - rather than a step in the algorithm. I naturally refactored the code after but it was implementing I found hard.

Original source

Related problems