It's vague how to write tests for classes that are reorganized to compose other classes.

I have not read widely or completely about TDD, but I feel that I know the basics.

One point of TDD is that you write tests first. In the case of a calculator program, perhaps one test case looks like this:

public class CalculatorTest { public void shouldSumOneAndOneCorrectly() { Calculator calc = new Calculator(); calc.push(new Number(1)); calc.push(new SumOperator()); calc.push(new Number(1)); Number sum = calc.equals(); assert(sum.equals(new Number(2))); } } 

If I write the above test before writing any implementation, then I am faced with this problem: am I testing if the class of the calculator is summed correctly, or should I check the correctness of the sum of the Number class? Of course, this assumes that the Calculator class will call the number.add (number) function in its implementation somewhere. But I thought you shouldn't think about implementation when writing your tests first?

If I start writing tests for the calculator class, and then during implementation, they will understand that my Calculator class simply delegates and coordinates folded objects, then I write tests for those composite objects? Hold the calculator tests?

You start writing tests at a very high level (Calculator), and then when you implement the functionality and create grouped classes, write more tests for these classes?

Hope I make sense.

0
source share
1 answer

I would start by focusing on behavior.

You want your Calculator class to be able to add 2 numbers. Its (pretty) doesn't matter if you do it through some other class, as this is a part of the calculator implementation. Your tests should give you an idea of ​​the Number class if you implement functionality there.

If you decide that the functionality of the Number class is something that is generally useful outside the Calculator (and is no longer an implementation detail), then this is the point at which I would consider writing tests based on the behavior for the Number class separately, but to this point, I would rely on Calculator behavior tests.

For me, this is similar to testing for a public method that delegates private methods to do the job. You do not want to write tests for private methods, because they must be covered by tests for the behavior of public methods.

+2
source

All Articles