mardi 14 avril 2015

How do you test a loop with TDD?

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.wasProccessed(a);
}


The logical next test is:



processesInput2() {
// Arrange
inputQueue = [a, b];

// Act
methodUnderTest(inputQueue);

// Assert
Assert.wasProccessed(a);
Assert.wasProccessed(b);
}


but by the time we get to:



processesInput3() {
// Arrange
inputQueue = [a, b, c];

// Act
methodUnderTest(inputQueue);

// Assert
Assert.wasProccessed(a);
Assert.wasProccessed(b);
Assert.wasProccessed(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?


Aucun commentaire:

Enregistrer un commentaire