Several of the books I’ve read lately have been mentioning different ways to apply test driven development to your code but they really don’t talk about how to set that up in the IDE. I’m a vs2005/vs2008 user so I thought I’d describe how I set that up with respects to using NUnit. Eventually I’ll cover the VSTS unit testing (once I’ve actually used it on rosario) and the vs2008 testing (good idea Brent).
I’m going to quote from Robert Martin’s book: “Agile principles, patterns, and practices in C#”.
Let’s say we did a whiteboarding session and came up with the following UML:
If we sat down and decided to write a unit test specifically for Payroll we’re going to have some serious problems because we can’t do so without also writing Employee Database, Checkwriter, or Employee.
Basically, in bob's book he says you'll eventually come up with a design like this that would fix those dependencies to allow you to unit test:
you can now build a test like this:
public class PayrollUnitTest
public void TestPayroll()
IEmployeeDatabase db = new MockEmployeeDatabase();
ICheckWriter writer = new MockCheckWriter();
Payroll payRoll=new Payroll(db,writer);
(writer as MockCheckWriter)
(db as MockEmployeeDatabase)
Of course, it doesn’t build because we don’t have the supporting objects or method stubs, so let’s do that…(note I’m using interface references so I can auto stub the interface methods and then ‘as’ casting them so I can stub the mock methods)
So once i stubbed things in and added my objects i get the following project setup. I’ll leave the thinking of implementation up to you.
download the source