Let's write our first test. In this test we score a game where every throw was a zero (gutterball).
At this point we cannot even run the test because the compiler is returning an error. We need to add a Game class.
- Add a Game class to the BowlingGame project.
- Add a reference to the BowlingGame project in the BowlingGame.Test project.
The next step is to roll 20 zeros.
The Roll method does not exist, so we cannot compile.
Add the Roll method in BowlingGame.Game.
Running the tests results in the following error:
1 test failed "System.NotImplementedException"
We need to get the test passing with as little code as possible. This helps us take small, incremental steps and keeps our code always working.
So, what is it going to take to get this test passing? It's tempting to go ahead and code the scoring ability. Doing that would require more code than what would be needed to get the test to pass.
Let's take a look at Uncle Bob's Transformation Priority Premise. The first transformation we should try is "nil". Can we get the test to pass by doing nothing?
We remove the exception and run the test again.
1 test passed
Now that we have a passing test, we can refactor. This is the Red, Green, Refactor pattern from Kent Beck's book, Test Driven Design by Example.
Now we can continue with the gutterball test.
We made our first assertion. We assert that we expect zero to be the returned value of scoring the game. The score method does not exist. To get the code to compile we need to add the Score method.
Unfortunately doing "nil" will not work this time. So, let's see what's next on Uncle Bob's Transformation Priority Premise. It's "constant". Can we return a constant to get the test to pass? Yes, we can return a zero.