In my earlier post, I defined what Behavior-Driven Development (BDD) was and threw in a code example using RhinoMocks. Hopefully, I was able to inspire some of you out there. In the event that I did and you will actually attempt to adopt this methodology in your enterprise, I will walk you through the steps required to smoothly integrate this into your existing development environment.
What You Will Need
- Microsoft Visual Studio 2008 or 2010 Test Edition or above
- Team Foundation Server
- This is optional if you want to into take advantage of MSBuild running your tests on check-in.
- RhinoMocks – (Download Link)
- TestDriven.NET – (Download Link)
- This is also optional. TestDriven.NET is an add-in testing component for Visual Studio that makes running your tests a breeze.
- The personal edition is free for open source users, trial users, and students.
Step 1 – RhinoMocks
Download RhinoMocks. The zip file does not contain an executable. It’s just a library that you will reference in your project. You can put it in a new directory someone on C:\ for now.
Step 2 – TestDriven.NET
Download and install. Again, this is an optional component but you should definitely give it a “test drive” and decide, if you are in a company setting, whether you want to purchase a license. It my example, I will be using MSTest, but it supports multiple test frameworks like NUnit.
Step 3 – Create an empty C# class library project

This will be the project you will be testing against. You can add it to source control if you have a connection to Team Foundation Server.
When you create it, it should also create a solution.
Step 4 – Create another empty C# class library project
This will be the test project. Make sure to stick to a consistent naming convention. I end my test projects with “.UnitTests” in the name (e.g. BLL.ShoppingCart.UnitTests). This allows you to specify a wildcard pattern in MSBuild when running tests. I’ll go into this in more detail below.
Step 5 – Add Rhino.Mocks.DLL reference to test project
Right click on References and click Add Reference. Locate the RhinoMocks DLL file you extracted in the first step and add it to the project.
Step 6 – Write your test and build solution
When copying my code, please replace the less than/greater than signs with brackets. The code css styler had a problem with them.
using Rhino.Mocks;
namespace BLL.Cart.UnitTests
{
<TestClass>
public class CartTest
{
private MockRepository _mock;
<TestInitialize>
public void TestSetup()
{
_mock = new MockRepository();
}
<TestMethod>
public void WhenItemAdded_CartQuantityShouldIncrease()
{
//test code
} } }
Step 7 – Create Build Definition and Incorporate Test Assemblies
In Team Explorer, you will go through the regular motions of creating a build definition. The option that will trigger the tests is on the last option screen where you will indicate your test assembly pattern, as shown in the screenshot inset.
The last thing you will want to make sure is that the Rhino.Mocks DLL you included in the project is added to source control. Otherwise, when MSBuild attempts to build your solution, you will get a reference error and it will not build.
And that’s it.
As always, I’d love to hear your feedback to see if this was helpful or if there is anything I might have missed.

