Building a MVC2 Template, Part 5, Writing Specs

1 Comment

In our last installment we converted the default tests to specifications  for the Home and Account controllers. In this article we will be adding some meat to those specifications.

There are several good articles on the net about writing your specs. I stumbled across a couple more while preparing for this article. A reference list of all articles used in this post can be found at the end of the article.

There’s no need for me to repeat one of the best articles now. So go here, http://blog.wekeroad.com/2009/09/21/make-bdd-your-bff-2, and read this great post by Rob Conery.

The second article to read is http://telldontask.wordpress.com/2009/11/19/bdd-with-mspec-and-rhino-auto-mocks-part-2/.

We’ll combine what we learn in these two articles to write our specs.

First add a new code file to the Nehemiah.Specs project named TestExtensions as shown below. This comes from James Broome and JP Boodhoo via Tell don’t ask.

using System.Web.Mvc;

namespace Nehemiah.Specs
{

public static class TestExtensions
{
public static ViewResult is_a_view_and(this ActionResult result)
{
return result as ViewResult;
}
}

}

I’m not going into detail about writing our specs as this is covered in the two articles I mentioned above. You should read those now if you haven’t already.

Notice lines 30 and 57 both read result.is_a_view_and().ViewName.ShouldBeEmpty();. Since Mvc uses convention over configuration we do not have to specify the name of the view when calling View in our actions.  It will look for and find the view with the same name as the action.

using System.Web.Mvc;
using Machine.Specifications;
using Nehemiah.Controllers;

namespace Nehemiah.Specs.Controllers
{

[Subject("Home Page")]
public class when_the_home_page_is_requested
{
static HomeController controller;
static ActionResult result;
static string key;
static string message;

Establish context = () =>
{
controller = new HomeController();
key = "Message";
message = "Welcome to ASP.NET MVC!";
};

Because of = () =>
{
result = controller.Index();
};

It should_return_the_home_page = () =>
{
result.is_a_view_and().ViewName.ShouldBeEmpty();
};

It should_display_a_welcome_message = () =>
{
result.is_a_view_and().ViewData[key].ShouldEqual(message);
};
}

[Subject("About Page")]
public class when_the_about_page_is_requested
{
static HomeController controller;
static ActionResult result;

Establish context = () =>
{
controller = new HomeController();
};

Because of = () =>
{
result = controller.About();
};

It should_return_the_about_page = () =>
{
result.is_a_view_and().ViewName.ShouldBeEmpty();
};

}

}

Report-html-02
Rebuild the solution. Now highlight the Nehemiah.Spec project and run MSpec from the Tools menu.  When you open the report you should see that of 38 specifications, 35 are not implemented.

Next commit all your changes to Subversion. Then open a browser to http://localhost:8080 to see if TeamCity build your solution successfully. You may have to wait a few minutes depending on how often you configured TeamCity to check for updates.  If you have followed along with this series of articles, the interval is five minutes.

Next Time

In the next post we will finish the writing the AccountController specifications.

References

Shout it


Kick It on DotNetKicks.com
Digg This
Reddit This
Stumble Now!
Buzz This
Vote on DZone
Share on Facebook
Bookmark this on Delicious
Kick It on DotNetKicks.com
Shout it
Share on LinkedIn
Bookmark this on Technorati
Post on Twitter
Google Buzz (aka. Google Reader)

One Comment (+add yours?)

Leave a Reply

Comment moderation is enabled. Your comment may take some time to appear.