20.8. Exercises: Unit Testing

In the last section, we walked through adding tests for checking the number of wheels in a car object. You are not required to have that code for the exercises. If you do have it in your code, you may need to adapt the exercises to adjust to the new property.

Work on these exercises in your local copy of the csharp-web-dev-lsn5unittesting repo. You were directed to fork and clone this repo earlier in the chapter. If you have not done so, do so now! Before starting work on the exercises, check out the exercises-start branch.

Tip

Earlier in the chapter, we instantiated an object of the Car class called test_car. Be sure to take note of the values of the make, model, gasTankSize, and milesPerGallon fields! Having a note you can quickly reference of how big the gas tank is will help you decide on values to use in your tests!

20.8.1. TestGasTankAfterDriving()

Add a test for the third TODO, “GasTankLevel is accurate after driving within tank range”.

  1. Your test must use the Car method Drive()

    test_car.Drive(50);
    
  2. With a value of 50 miles passed into Drive(), we expect test_car to have a GasTankLevel of 9.

    Assert.AreEqual(9, test_car.GasTankLevel, .001);
    

20.8.2. TestGasTankAfterExceedingTankRange()

Add a test for the fourth TODO, “GasTankLevel is accurate after attempting to drive past tank range”.

  1. You’re on your own for this one. You’ll need to simulate the Car travelling farther than it’s gasTankLevel allows.

20.8.3. TestGasOverfillException()

The test for our last TODO is a little different. We are going to perform an action on our car object, and we are expecting the object to throw an error. In this case, we are going to attempt to add gas to our car that exceeds the gas tank size.

  1. First, we’ll add our [TestMethod] annotation to tell MSTest this is a test.

    //TODO: can't have more gas than tank size, expect an exception
    [TestMethod]
    public void TestGasOverfillException() {
    
    }
    
  2. Now we need to tell MSTest the test passes if an exception is thrown. We will use a new attribute [ExpectedException].

    //TODO: can't have more gas than tank size, expect an exception
    [TestMethod]
    [ExpectedException(typeof(ArgumentOutOfRangeException))]
    public void TestGasOverfillException() {
    
    }
    
  3. Update the Car class to include an AddGas() method.

    public void AddGas(double gas) {
      GasTankLevel += gas;
    }
    
  4. Back in CarTests, implement the new AddGas() method and a Assert.Fail() scenario.

    test_car.AddGas(5);
    Assert.Fail("Shouldn't get here, car cannot have more gas in tank than the size of the tank");
    
  5. Run the test. It should fail! In the output log, we can see our Assert.Fail() statement about not being able to add more gas printed out.

  6. We need to refactor Car to throw an exception when too much gas is added to the tank. Find the AddGas() method and modify it by adding the following code in the appropriate place.

    if (GasTankLevel > GasTankSize)
    {
       throw new ArgumentOutOfRangeException("Can't exceed tank size");
    }
    
  7. Now, run the test - it should pass!