In this tip, I discuss Eric Hexter’s TDD Class Generator Add-In for Visual Studio. This Add-In enables you to generate a class, an interface, and a test class by entering a single keyboard shortcut.
Eric Hexter -- who has done valuable work on the MvcContrib project -- visited Microsoft last week and showed me one of his current projects. He has started a project for building ASP.NET MVC specific Add-Ins for Visual Studio. In particular, he has created an Add-In for generating testable classes automatically.
You can learn more about the project, and download the binaries and source code, from the following website:
You also can view a quick video walk-through of the Add-In at:
Download the LatestVersion.zip file and unzip the archive. After unzipping the archive, make sure that you right-click the file, select Properties, and click the Unblock button (see Figure 1). If you forget to unblock the archive, then you’ll encounter security issues when attempting to use the Add-In.
Figure 1 – Unblocking the Archive
After you unzip the Add-In, you must install the Add-In in Visual Studio. The download includes an Install.cmd batch file for installing the Add-In. Unfortunately, however, the installer did not work on my machine because of path issues. Therefore, I followed these steps to install the Add-In:
1. Create a new folder named AddIns in your Documents\Visual Studio 2008 folder.
2. Copy all of the unzipped files from the download into the new AddIns folder
3. After launching Visual Studio, select the menu option Tools, Add-In Manager and enable the TDD Class Generator Add-In (see Figure 2).
Figure 2 – Enabling the TDD Class Generator Add-In
Admittedly, the project is very much in the infant stage. Don’t expect a fancy interface.
Before you can use the TDD Class Generator, you first must create a new C# ASP.NET MVC Web Application. Make sure that you create a test project. Also, you need to add references to both NUnit and Rhino Mocks to your test project.
After you create a new project, you use the TDD Class Generator Add-In by selecting the menu option Tools, Generate Class Under Test. Alternatively, you can just entry the keyboard combination Ctrl-Alt-Enter. Either method of launching the Add-In displays the dialog in Figure 4.
Figure 3 – Using the TDD Class Generator
Figure 4 – TDD Class Generator Dialog
Select the name of your application project and the name of your test project from the dropdown lists. If you complete the dialog by entering the value Models for Folder location and Movie for Class name and click the Generate button (well, the Generat button on my machine) then the following files are generated in the application project automatically:
And, the following file is generated in the test project automatically:
The goal of the tool is to set you on the right path. It doesn’t just create a Movie class. Instead, it creates a Movie class that implements an interface. It also assumes that you will need to mock the class. Therefore, the TDD Class Generator adds the mocking logic to the MovieTester.cs class automatically.
I think Eric Hexter’s approach is a great approach for making it easier to build ASP.NET MVC applications. Right now, there is a lot of repetitive file creation tasks involved when getting a new ASP.NET MVC application off the ground (Especially if you want to do things the right way). By building a library of ASP.NET MVC specific Add-Ins, you can eliminate these brainless, tedious tasks.