Solution: Unit Test Using Test After Development
Learn the solution to the test after development challenge and learn from your mistakes.
We'll cover the following...
Introduction
This lesson presents the solution to the challenge to implement a simple probability calculator, followed by the test methods to verify its correctness.
Solution to challenge
The project solution is shown in the widget below. Run the widget to see that all tests pass:
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace Project { public class Calculator { public int Factorial(int n) { if (n < 0) { throw new ArgumentException(); } //The if statement below caters for the special case where the //input is the value of zero if (n == 0) { return 1; } for (int i = n - 1; i > 0; i--) { n *= i; } return n; } public int Npr(int n, int r) { if (n < 0 || r < 0 || r > n) { throw new ArgumentException(); } return Factorial(n)/Factorial(n - r); } public int Ncr(int n, int r) { if (n < 0 || r < 0 || r > n) { throw new ArgumentException(); } return Factorial(n) / (Factorial(r) * Factorial(n - r)); } } }
The solution to the challenge
Code walkthrough
The application code contains a single class called Calculator
with three public methods:
Factorial
(line 11)Npr
(line 30)Ncr
(line 39)
The Factorial
method is not only a public method but is also used to calculate the return values of the Npr
and ...