Test-Driven Development Exercise
Task 1 Solution
So how did we TDD task 1? Click on the tabs below to find out...
Here is the first in a series of videos showing how we applied TDD to the problem at hand. The video is about 9 minutes long.
public class Account
{
private readonly int numberOfLines;
private readonly double monthlyRate;
private readonly double additionalLineRate; public Account(int numberOfLines, double monthlyRate, double additionalLineRate)
{
this.numberOfLines = numberOfLines;
this.monthlyRate = monthlyRate;
this.additionalLineRate = additionalLineRate;
} public double ComputeBill()
{
return monthlyRate + (numberOfLines - 1)*additionalLineRate;
}
}
[TestFixture]
public class AccountTest
{
private static readonly int ONE_LINE = 1;
private static readonly int TWO_LINES = 2;
private static readonly int THREE_LINES = 3;
private static readonly double GOLD_MONTHLY_RATE = 49.95;
private static readonly double GOLD_ADDITIONAL_LINE_RATE = 14.50;
private static readonly double SILVER_MONTHLY_RATE = 29.95;
private static readonly double SILVER_ADDITIONAL_LINE_RATE = 21.50; [Test]
public void GoldAccount()
{
Assert.AreEqual(49.95, GoldAccountWith(ONE_LINE).ComputeBill());
Assert.AreEqual(64.45, GoldAccountWith(TWO_LINES).ComputeBill());
} [Test]
public void SilverAccount()
{
Assert.AreEqual(29.95, SilverAccountWith(ONE_LINE).ComputeBill());
Assert.AreEqual(72.95, SilverAccountWith(THREE_LINES).ComputeBill());
} private Account GoldAccountWith(int numberOfLines)
{
return new Account(numberOfLines, GOLD_MONTHLY_RATE, GOLD_ADDITIONAL_LINE_RATE);
} private Account SilverAccountWith(int numberOfLines)
{
return new Account(numberOfLines, SILVER_MONTHLY_RATE, SILVER_ADDITIONAL_LINE_RATE);
}
}
Click on the link below to download the source code for this task.
(Reading...)
Formatting Hints:
To format... | use this markup: |
---|---|
source code | {source:lang=java|lineNumbers=false}...{source} (Supported languages: java, cpp, csharp, c, python.) |
bold text | __some text__ |
italicized text | ~~some text~~ |
underlined text | @@some text@@ |
a method or class name | ``myMethod(...)`` |