BDD with SpecFlow, NUnit and MVC3–Getting Started

Up to now I have been using Test Driven Development (and occasionally just Unit Testing after the fact) but I want to start using Behaviour Driven Development as it seems a more natural way to flesh out an application.

When using TDD, code is tested one unit (class or method) at a time, any dependencies are mocked, faked, or stubbed out, so any errors are within the unit being tested. With BDD testing is done one feature at a time from the outside in, starting at or near the interface and progressing through further tests down toward the database.

With MVC there is a choice of starting with the interface using a framework such as Watin or just starting with the controller. I intend to start with the controller, if I need to test routing I will do so separately. At the other end I will test down as far as the repository, faking the database context.

Getting Set Up

I’m using Visual Studio 2010 so will take advantage of the Nuget package manager. I have also decided to use SpecFlow as my BDD framework of choice, I am sure other frameworks are equally as good. Whilst there is a package for SpecFlow, it doesn’t seem to add all it should at the moment (VS templates), so it is worth downloading and running the installer from

Now create a new MVC3 razor project in Visual Studio (no test project), then create a second, class assembly, project in the solution named Specs. Right click the Specs project and select Add Library Package Reference. From the Nuget dialog install:

  • SpecFlow
  • NUnit
  • Moq
  • Should
  • ShouldFluent

SpecFlow can use most of the testing frameworks, my preference is for NUnit. I have not used Should before but it looks a great way to write more readable (therefore more maintainable) tests.

In the next post I’ll start my first feature (view a list of products).

2 thoughts on “BDD with SpecFlow, NUnit and MVC3–Getting Started

  1. Can we use MVc3 razar in bdd having JSON Results Contoller & having js functions on view

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.