My Take On JPeck’s TDD Example

9 Mar

I had some spare time today, so I caught up on my friend James’ blog. He recently posted a real-world example of TDD taken, I assume, from his real-world job of developing software for a bank.

In the comments section of this particular post, someone named Darren made a case for using interfaces and mocks in the design of the tests and the resulting software. James stated that he would probably try that approach next. Well, the original post is about one month old now and I happen to have T.A.D.D. (Technical Attention Deficit Disorder). So, I’ve posted my version of James’ real-world TDD example. A few comments about my design:

  • I changed the design of the HumanResourcesService. I renamed it to “IEmployeeService”, extracted and renamed the methods and created a criteria-based lookup.
  • I renamed the “HumanResources” class to “EmployeeManager” and made it work against the IEmployeeService abstraction.
  • I exposed the GetEmployeeById as a public method in EmployeeManager. I figured this would be a good thing to provide consumers.
  • I used Rhino Mocks and SpecUnit as the mocking and TDD frameworks, respectfully.
  • I generated a report (HR.Specs.html) showing the different specifications the test code is exercising. The report looks nice, but to get it I had to use underscores in the spec class names (yuck). Was it worth it? Probably.

In general, I tried to follow the AAA (Arrange, Act, Assert) pattern in the spec code. I’ve included James’ very-slightly-modified code in the solution for reference (I renamed a couple of variables I found offensive). Click here to download the code. Rename it to a .zip file to access the contents.

4 Responses to “My Take On JPeck’s TDD Example”

  1. jhered March 9, 2010 at 1:50 pm #

    that’s pretty slick. i like how the spec reads. Good job!

    Is there equivalent .net 1.1 tools like this?

  2. jhered March 9, 2010 at 1:54 pm #

    i have another question. how do you handle file naming convention so that people can find the tests easy? my issue with naming classes in such a way is that it’s hard to find the files you are interested in when adding more tests. (aside from just making your change and seeing what breaks)

  3. jhered March 9, 2010 at 2:02 pm #

    doh i just checked it out it’s using extension methods 😦 .net 3.5 or better only.

    that being said the class/method naming convention you’re using is cool and would work just fine in nunit i believe…

    [TestFixture]
    public class When_I_Fire_An_Employee
    {
    [Test]
    public void The_Termination_Date_Should_Be_Changed_To_Today()
    {

  4. cmleon March 9, 2010 at 2:15 pm #

    You got it.

Leave a reply to jhered Cancel reply