mercredi 12 août 2020

Refactoring TDD down to an array of tests

So, I have a JUnit test for Uncle Bob's classic Bowling game example for TDD.

I refactored the test to use an array of games and expected scores.

The advantage is it's easy to add new tests.

The disadvantage is that it doesn't "self document" the code or the test.

Are there any best practices surrouding this?

public class ScoreTest {
int[][] games = {
  {0,0,0,0,0,0,0,0,0,0},
  {10,10,10,10,10,10,10,10,10,10,10,10
};
int[] scores = {0, 300};
@Test
public void testScore() {
  for(int i=0; i<games.length; i++) {
    let game = games[i];
    let expectedScore = scores[i];
    let score = new Score();
    score.roll(game); // roll the entire game
    let actualScore = score.total() // calculate the bowling score
    assertEquals(expectedScore, actualScore);
  }
}
}

Aucun commentaire:

Enregistrer un commentaire