Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

QL-4.1) Setup and first test

  1. Create a new Test Project in the FindHighestNumber Solution

  2. Install the packages you want (MSTest already installed, or NUnit - we are going to be using NUnit)

  3. Create a new setup the new test class as shown here (remember you will have errors)

  4. Code Block
    languagec#
    namespace TopicManagerTests
    {
        public class TopicManagerTests
        {
            [Test]
            public void find_heighest_score_in_empty_array_return_empty_array()
            {
                // Arrange
                int[] array = {};
                TopicManager cut = new TopicManager();
                int[] expectedResult = {};
    
                // Act
                Topic[] result = cut.findTopicHighScores(array);
    
                // Assert
                Assert.That(result, Is.EqualTo(result));
            }
        }
    }
  5. Create a new class called TopicManager in the FindHighestNumberService project

  6. Clean the code up so that the class is in a namespace called TopicManagerService

  7. Lines 9, 11, and 14 give us enough information to allow us to start thinking about what are trying to design here. based on the requirements, we want to pass into the method findTopicHighScores an array of Topics and their accompanying scores. It should return the topic score of each Topic, we will call this TopicTopScore. Each item in the array being passed into the findTopicHighScores will be called TopicScores (plural I know, but it matches the context, you many want to lose the ‘s' at the end of the class name). Let’s refactor the code reflect what we have outlined here.

  8. Code Block
    languagec#
        public class TopicManagerTests
        {
            [Test]
            public void find_heighest_score_in_empty_array_return_empty_array()
            {
                // Arrange
                TopicScores[] array = Array.Empty<TopicScores>();
                TopicManager cut = new TopicManager();
                TopicTopScore[] expectedResult = Array.Empty<TopicTopScore>();
    
                // Act
                TopicTopScore[] result = cut.findTopicHighScores(array);
    
                // Assert
                Assert.That(result, Is.EqualTo(result));
            }
        }
  9. We are now in a good position to get VS to generate the findTopicHighScores with the correct signature

  10. Code Block
    languagec#
    namespace TopicManagerService
    {
        public class TopicManager // TopicManager.cs
        {
            public TopicTopScore[] findTopicHighScores(TopicScores[] array)
            {
                // This block oc code assumes that the input is an empty array, there is no need for an if statement
                return Array.Empty<TopicTopScore>();
            }
        }
    }
  11. Now we need to complete the first piece of implementation code to pass the test

  12. So you should have the following pieces of code

  13. Code Block
    namespace TopicManagerService
    {
        public class TopicTopScore // TopicTopSscore.cs
        {
        }
    }
  14. Code Block
    namespace TopicManagerService
    {
        public class TopicScores // TopicScores.cs
        {
        }
    }

...

Tip

Tests are a great way of identifying code smells. Highly coupled code leads to untestable code.

You want to test one class and one class only. The previous version of the TopicManager dragged in HighestNumberFinder. So inadvertently you were testing that class as well. This will become clearer as we continue to work through the TopicManager tests.

Working with Stubs

Info

A Stub is part of the family of Test Doubles. They are used to ensure that tests focus on the behaviour of the CUT and not its dependents. Test environments should be controlled and predictable. Test Doubles give you that measure of stability and predictability.

A Stub method is one that returns canned results. A canned result is a predefined result. The result can be specific, a range of values, or any value. Also, the parameters into the method can be specific value, a range of values, or any value.

...

Info

We’ve now created a controlled environment for our tests

...

QL-4.2 Stubs

Add more tests to handle the last two requirements

...