Step by Step BDD with ASP.NET MVC 4 SpecFlow And MsTest – Part 2

In part one I showed how to integrate Visual Studio with SpecFlow and how to setup MsTest as our preferred test framework.

In this post we are gonna implement a basic functionality following the BDD principles.

1 – Write a failing test

Firstly, we need to specify “what” the system needs to “do”. And this specification is written in
Gherkin Language

Feature: HomePage
	In order to access the best mvc application in the world
	As a web user
	I want to access the home page

Scenario: Default Homepage Message 
	
When The User Is In The Homepage
Then The Page Title should say "Welcome to this wonderful site!"

1.1 – Add a .feature file

Add a Feature File
Add a Feature File

The new .feature file contains a default text that comes with the template. Lets replace its content with our new feature and scenario as follows

Your feature should look like this
The new feature file ready to start the test implementation.

1.2 – Add the step definitions

Now that we have told “what” the system needs to do, we need to add the necessary code to make the acceptance test work.

This is done by adding an Steps Class, which contains one method per step. As the scenario we are working on contains 2 steps, the corresponding class will have 2 test methods.

SpecFlow’s Visual Studio Integration provides a context menu to create all this stuff for us.

Choose Generate Steps Definition from the Context Menu
Choose Generate Steps Definition from the Context Menu

The Steps Definition Wizard will show the steps to be generated. Click on “Generate” to create the Steps Class.

Click on Generate to create the steps class.
Click on Generate to create the steps class.

1.3 – Make your test fail

Your Steps class should look like this

    [Binding]
    public class HomePageSteps
    {
        ActionResult _actionResult;
        HomeController _controller;
        
        [When(@"The User Is In The Homepage")]
        public void WhenTheUserIsInTheHomepage()
        {
            _controller = new HomeController();
            _actionResult = _controller.Index();
        }
        
        [Then(@"I should see a Message that says ""(.*)""")]
        public void ThenIShouldSeeAMessageThatSays(string p0)
        {
            var viewBag = (_actionResult as ViewResult).ViewBag;
            var actual = viewBag.Message;

            Assert.AreEqual(p0, actual);
        }
    }

Not only you’ll be able to build your application without problems, but also you will see your test running and failing (As expected).

Run the test to see how it fails
Run the test to see how it fails

2 – Implement the necessary changes to make the test pass

Notice that at this point we did not write a single line of implementation code. The only one thing we have completed is the Acceptance Test.

So, the next step is to write enough code to make the test pass.

2.1 – Write the implementation code

Go to the HomeController and change the message.

    public class HomeController : Controller
    {
        public ActionResult Index()
        {
            ViewBag.Message = "Welcome to this wonderful site!";

            return View();
        }

2.2 – Make the test pass

Now build the solution and run the test again to see it passing.

Run the test again to verify it passes
Run the test again to verify it passes

3 – Refactor

Congrats! You’re done.

In more complex scenarios, this phase would be useful to clean up your code, make it easier to test, easier to maintain, etc.

Thanks for reading!

References

Principles of BDD at Wikipedia

Gherkin Language

One thought on “Step by Step BDD with ASP.NET MVC 4 SpecFlow And MsTest – Part 2”

Leave a Reply