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

ASP.NET MVC 2, MSpec and Watin