May
19
2010

ASP.NET MVC 2, MSpec and Watin

The other day I posted about “Automated functional tests using Watin and MSpec” which we do at Jobping as a spike to automate our functional tests on our ASP.NET MVC 2 site.

Yesterday evening I was facing an issue in my base class WebBaseSpec which led to really strange side effects. Basically when I was running one unit test alone it was Green, running all or more than one unit test will fail miserably with the well known STA issue of Watin.

So I thought that I had an issue with the ReSharper MSpec plugin but after some discussion with Alexander GroßI realized that the second failing test was showing another issue than the STA issue.

Going further I realized that when I was checking the following

It should_direct_user_to_aboutus_page = () =>
    Browser.Uri.Route().ShouldMapTo<HomeController>(x => x.About());

First I needed to call the ASP.NET MVC RegisterRoutes

MvcApplication.RegisterRoutes(RouteTable.Routes);

which was done in the constructor of my WebBaseSpec class.

protected WebBaseSpec()
{
    MvcApplication.RegisterRoutes(RouteTable.Routes);
    InitBrowser();
}

That’s was the problem, I was registering the routes several time, one time per test. So first one was ok, second one was failing…

So I modified it to the following, ensuring that the routes were registered only one time!

private static bool registered;

/// <summary>
/// Initializes a new instance of the <see cref="WebBaseSpec"/> class.
/// </summary>
protected WebBaseSpec()
{
    if (!registered)
    {
        MvcApplication.RegisterRoutes(RouteTable.Routes);
        registered = true;
    }
    InitBrowser();
}

Now I can run all my functional tests again

4621024158_d2c99039a0_o[1]

Add comment

  Country flag

biuquote
  • Comment
  • Preview
Loading

About Laurent

Laurent Kempé

Laurent Kempé is the editor, founder, and primary contributor of Tech Head Brothers, a French portal about Microsoft .NET technologies.

He is currently employed by Innoveo Solutions since 10/2007 as a Senior Solution Architect, certified Scrum Master and Founding Member.

Founder, owner and Managing Partner of Jobping, which provides a unique and efficient platform for connecting Microsoft skilled job seekers with employers using Microsoft technologies.

Laurent was awarded Most Valuable Professional (MVP) by Microsoft from April 2002 to April 2012.

JetBrains Academy Member
Certified ScrumMaster
My status

Twitter

Flickr

www.flickr.com
This is a Flickr badge showing public photos and videos from Laurent Kempé. Make your own badge here.

Month List

Page List