TDD Project and Item Templates (for Visual Studio 2015)



1. Introduction
2. System Requirements
3. Download and Install
4. Package Contents
4.1. Test Class Library Project Template 4.2. Test Class ItemTemplate 4.3. Code Snippets 5. Uninstall 6. Suggested Books

1. Introduction

If you've been using 'NUnit' you found yourself involved in doing repetitive actions like creating test projects and adding test classes and test methods. If you ever had a try with the 'Rhino Mocks' mocking framework you had an extra bit of repetitive code to write, in all of your TestFixture classes. I was tired of always creating a new test project for each project in my solution and manually add references. I was tired of always prepare test classes with Setup and Teardown methods as well as other standard elements. Then I created a standard Project Template creating a test class library, an Item Template creating a Rhino Mocks enabled TestFixture class, and a couple of useful code snippets. All this stuff is targeted to Visual C# projects.

2. System Requirements

You'll need the following software: Visual Studio 2015 Nunit (Nunit.3.9.0) -- you can find it via Nuget Rhino Mocks (RhinoMocks.3.6.1) -- you can find it via Nuget Important: Project references are based on the above specified Nunit and Rhino.Mocks versions installed via Nuget. Note: If your installations are different from the ones specified, you will need to modify project references once created your project with the 'project template' provided. Otherwise you can download my GitHub project, modify the template project and generate a new .vsix file.

3. Download and Install

First of all download the file: TDDTemplates.vsix that you can find here. All you have to do is download, double click, and proceed with the installation. If you want more detailed description of what you are downloading, read further. The file TDDTemplates.vsix is a Visual Studio installer file, containing:
  1. A Project Template named Test Class Library;
  2. An ItemTemplate named Test Class;
  3. Two Code snippets useful for test driven develpment (TDD Snippet component).
Double-clicking on the installer file you will get a dialog window prompting you to choose Visual Studio versions for wich to proceed. Click on the install button and you're done. → go to top

4. Package Contents

4.1. Test Class Library Project Template 4.2. Test Class ItemTemplate 4.3. Code Snippets In this paragraph you will find a detailed description of the contents installed by the Visual Studio installer we're talking about: TDDTemplates.vsix.

4.1. Test Class Library Project Template

When creating a new project, the initial dialog will list a new project template under the Visual C# category: Test Class Library, as shown in the following image.   Choose a name for your project, say: ProjectName and click ok. A new class library project will be created with the structure shown in the following screenshot of the Solution Explorer window: The file named TestClass.cs, created inside the project, contains the code defining the skeleton of a TestFixture class definition:
using System;
using System.Collections.Generic;
using System.Text;

using NUnit.Framework;
using Rhino.Mocks;

namespace ProjectName
{
    [TestFixture]
    public class TestClass
    {
        MockRepository mocks;

        /// <summary>
        /// Prepares mock repository
        /// </summary>
        [SetUp]
        public void Initialize()
        {
            mocks = new MockRepository();
        }

        /// <summary>
        /// template behavior and state testing method
        /// </summary>
        [Test]
        public void TestMethod1()
        {
            IDependency dependency = mocks.CreateMock<IDependency>();

            // Record expectations
            using (mocks.Record())
            {
                Expect.Call(dependency.Method1("parameter")).Return("result");
                dependency.Method2();
            }

            // Replay and validate interaction
            Subject subjectUnderTest;
            using (mocks.Playback())
            {
                subjectUnderTest = new Subject(dependency);
                subjectUnderTest.DoWork();
            }

            // Post-interaction assertion
            Assert.That(subjectUnderTest.WorkDone, Is.True);
        }
    }
}
As you can see the class definition already contains a default implementation of a Test method. Such method includes the definition of RhinoMocks expectations for a hypothetical object under test. I chose to use Rhino Mocks Record-playback Syntax which takes care of calling mocks.ReplayAll() and mocks.VerifyAll() under the hood. I feel this syntax is very neat and clean providing a clear separation between the moment in which we record expectations and the moment in which we use the object under test.
→ top of paragraph → top of post

4.2. Test Class Item Template

When adding a new item to the project, the Add New Item dialog will list a new project template: Test Class. It is possible that you have to scroll down a little to see the My Templates category, as shown in the following image: Selecting the Test Class template, a new class definition file will be created, containing the same class definition you obtained when creating the Test Class Library Project (see previous paragraph). → top of paragraph → top of post

4.3. Code Snippets

From now on, when writing code, you will have two more intellisense entries in your IDE: TDDtestmethod and TDDusing. TDDtestmethod will insert a test method definition as shown in the following sequence of images: Selecting the keyword as in the preceding image will produce the following code:
[Test]
public void TestMethod()
{
}
The snippet allows the user to choose the method's Attribute (defaults to: Nunit.Framework.TestAttribute) and the methods name (defaults to: TestMethod). TDDusing will insert the two using blocks needed for the Rhino Mocks Record-playback Syntax, as shown in the following sequence of images: Selecting the keyword as in the preceding image will produce the following code:
using (mocks.Record())
{

}

using (mocks.Playback())
{
}
The snippet allows the user to choose the instance of Rhino.Mocks.MockRepository to use for mocking objects. → top of paragraph → top of post

5. Uninstall

If you want to uninstall the installed content you can simply go to the Extensions and Updates dialog box, identify the following entry and click the Uninstall button. That's all. Feel free to post a comment if you want to report a bug or to propose enhancement of the templates and snippets. → top of paragraph → top of post