I am working on an Intro to TDD hands-on workshop, so I created a project and started to go through and complete the user stories using Test-Driven Development to make sure there weren’t any major gotchas or anything silly or anything I missed. I thought that this would be a great time to share what steps I took. The project is for a bank account.
If you are not familiar with TDD, you can check out the webinar series here. Or, wait for the class to become available to the public.
I got started with an empty project. I went ahead and added my code to Surround SCM and set up Hudson for continuous integration. I am using csUnit 2.6 for my unit tests. Time to get started.
Remember the goal of TDD is do as little as possible to get the test to pass (and not break any other test).
The first user story is: As a user, when I open an account, the initial balance should be zero so that I can start fresh.
My first task was to create a test that creates an account and ensures the balance is indeed zero.
[Test]
public void testInitialBalanceZero()
{
BankAccount account = new BankAccount();
Assert.Equals(0,account.getBalance());
}
Because there is no BankAccount class yet, this fails.
error CS0246: The type or namespace name ‘BankAccount’ could not be found (are you missing a using directive or an assembly reference?)
Now I need to resolve the errors. First, I create a BankAccount class.
The latest error is:
error CS0117: ‘Project.BankAccount’ does not contain a definition for ‘getBalance’
I right-click on getBalance() and generate a method stub. The method stub that Visual Studio gives me looks like this:
internal double getBalance()
{
throw new Exception(“The method or operation is not implemented.”);
}
Now my code builds successfully. One hurdle overcome.
========== Build: 1 succeeded or up-to-date, 0 failed, 0 skipped ==========
But my test fails!
I update the getBalance method:
internal double getBalance()
{
return 0;
}
SUCCESS!
TDD might be slower at first, but the more practice that you have, the better you get, and the faster it goes!


