BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Programming Concepts
.NET 3.5+

Test-Driven Development

Test-driven development is a process that assists in creating high quality, well designed, loosely coupled and maintainable code that can be refactored with confidence. The process relies on writing unit tests before creating the code that they validate.

Iteration Two

For our second iteration we'll add the ability to deposit money into the account. To write the test we need to think about how the deposit will be made. We could include a simple Deposit method that accepts a monetary value. As we know we will be creating a history of transactions, it would be useful to also pass in a description of the transaction as a string.

To tie the two values together, let's decide to create a Deposit method that accepts an object containing both the value and description together. This might make the code more maintainable. An obvious name for the class is Deposit.

The test below checks that we can deposit a value and have the balance increased accordingly. It doesn't check that the deposit is recorded or that the description is used. Again the code will not build because we have yet to create the Deposit class or the Account's Deposit method.

[Test]
public void DepositingIncreasesTheBalance()
{
    Account account = new Account();
    Deposit deposit1 = new Deposit(100M, "Opening Balance");
    Deposit deposit2 = new Deposit(23.45M, "Deposit");

    account.Deposit(deposit1);
    account.Deposit(deposit2);

    Assert.AreEqual(123.45M, account.Balance);
}

To make the solution compile we need to add the Deposit class. This is shown below. NB: I've used automatically-implemented properties for simplicity. For early versions of .NET you will need to expand these to read-only properties with a backing store variable.

public class Deposit
{
    public Deposit(decimal value, string description)
    {
        Value = value;
        Description = description;
    }

    public string Description { get; private set; }

    public decimal Value { get; private set; }
}

We also need to add the Deposit method to the Account class. As the method returns void, we don't need to add any code other than the signature. The empty method will ensure our test will fail.

public void Deposit(Deposit deposit)
{   
}

Running the tests should show that the new test fails but the earlier test is still green. To make all of the tests pass, we need to modify the Balance property so that it tracks a balance. We also need to add code to increase the balance to the Deposit method, as shown below:

public class Account
{
    public decimal Balance { get; private set; }

    public void Deposit(Deposit deposit)
    {
        Balance += deposit.Value;
    }
}

Adding this code causes the tests to pass. As the code is so simple, no refactoring is required in this iteration.

19 November 2012