...
...
...
...
...
...
...
...
...
...
...
...
An organisation delivers several topics (subjects). Students are graded against each topic. You are required to store the top score for each topic.
We’ve designed the application so that it comprises of three core classes:
A class to find the highest number from an array of integers.
A class to find the highest score for a topic.
A class to write the topic and score to a file on the disk.
...
Info |
---|
You are going to follow a TDD approach to finding the highest number in an array of integers |
Given the following specification
If the input were {4, 5, -8, 3, 11, -21, 6} the result should be 11
An empty array should throw an exception
A single-item array should return the single item
If several numbers are equal and highest, only one should be returned
If the input were {7, 13} then the result should be 13
If the input were {13, 4} then the result should be 13
Steps
Begin by creating a solution called FindHighesNumber with a testing project called FindHighestNumberTests (use the NUnit testing framework)
Rename the test to HighestNumberFinderTests
The most challenging part is determining which test to write first. Always start simple and with a test that will not need to handle exceptions.
So the simplest test we could do here is
A single-item array should return the single item
Write this first test, let’s call the test method array_of_one_item_returns_this_item()
Code Block language c# [Test] public void Array_of_one_item_returns_this_item() { // Arrange int[] values = { 33 }; int expectedResult = 33; HighestNumberFinder cut = new HighestNumberFinder(); // Act int result = cut.findHighestNumber(values); //Assert Assert.That(result, Is.EqualTo(expectedResult)); }
Create a new project called FindHighestNumberService in the same solution
Rename the production class to HighestNumberFinder
In the Test file HighestNumberFinderTests, right-click on the HighestNumberFinder declaration and select Add reference to HighestNumberFinder, the error should disappear
In the Test file HighestNumberFinderTests, right-click on the method findHighestNumber() and select Generate method
Now begin to work on the production code
Write enough production code to pass the test.
Do not be tempted to try and answer other parts of the requirements. Focus only on this requirement “A single item array should return the single item”
Code Block language c# using System; namespace FindHighestNumberService { public class HighestNumberFinder { public int findHighestNumber(int[] values) { return values[0]; } } }
Make sure the test passes
A Golden rule of TDD - if this was the only requirement then you have completed your task. Only write enough code to pass the test.
Commit your passing code to your git repo (never commit broken code)
Select the next requirement
I would suggest this one - If the input were {13, 4} then the result should be 13
Write the second test, let’s call the test method array_of_two_descending_items_return_first_item()
Code Block language c# [Test] public void Array_of_two_descending_items_return_first_item() { // Arrange int[] values = { 13,4 }; int expectedResult = 13; HighestNumberFinder cut = new HighestNumberFinder(); // Act int result = cut.findHighestNumber(values); //Assert Assert.That(result, Is.EqualTo(expectedResult)); }
Write enough production code to pass the test. In this edge case, the production code does not change
Make sure the test passes
Commit your passing code to your git repo (never commit broken code)
Select the next requirement
I would suggest - If the input were {7, 13} then the result should be 13
Write the third test, let’s call the test method array_of_two_ascending_items_return_last_item()
Code Block language c# [Test] public void Array_of_two_ascending_items_return_last_item() { // Arrange int[] values = { 7, 13 }; int expectedResult = 13; HighestNumberFinder cut = new HighestNumberFinder(); // Act int result = cut.findHighestNumber(values); //Assert Assert.That(result, Is.EqualTo(expectedResult)); }
Write enough production code to pass the test. Do not be tempted to try and answer the parts of the requirements. Focus only on this requirement “If the input were {7, 13} then the result should be 13”
Code Block language c# public int findHighestNumber(int[] values) { int result = values[0]; if (values.Length > 1 && values[1] > result) result = values[1]; return result; }
Make sure the test passes
Commit your passing code to your git repo (never commit broken code)
The Production code has changed. Does it need to be refactored?
if yes, refactor the code
Make sure the test still passes
Commit your passing code to your git repo (never commit broken code)
Select each requirement and implement the test first then the production code
The steps above are known as RED, GREEN, REFACTOR
Info |
---|