I often find that I am unsure how to test code that needs to loop. Take for example the following:
methodUnderTest(inputQueue) {
while (inputQueue.count > 0) {
process(inputQueue.dequeue());
}
}
The first test might look like this:
processesInput1() {
// Arrange
inputQueue = [a];
// Act
methodUnderTest(inputQueue);
// Assert
Assert.wasProcessed(a);
}
The logical next test is:
processesInput2() {
// Arrange
inputQueue = [a, b];
// Act
methodUnderTest(inputQueue);
// Assert
Assert.wasProcessed(a);
Assert.wasProcessed(b);
}
but by the time we get to:
processesInput3() {
// Arrange
inputQueue = [a, b, c];
// Act
methodUnderTest(inputQueue);
// Assert
Assert.wasProcessed(a);
Assert.wasProcessed(b);
Assert.wasProcessed(c);
}
It all starts to feel a little redundant. A good piece of advice I once heard about TDD was to treat tests like a specification. At what point does the test specify that an input of N items will all be processed? How best to portray this in a test?