Unit Testing Routes in Web.Config in ASP.NET MVC

I’ve spent the better part of my week working on testing routes in ASP.NET MVC. When the routes are in the Global.asax.cs file, it’s no big deal. Simply register the routes, and then assert as necessary against the RouteTable.Routes items. The tougher part comes when the routes are in the web.config (http://weblogs.asp.net/fredriknormen/archive/2008/03/11/asp-net-mvc-framework-2-define-routes-in-web-config.aspx) or in an external config file.

The preferred method is to set up a separate project for Unit Tests, and pulling in the routes from there. But, if you’re using JetBrains Resharper, you may just have your tests in a .cs file, with no class library project backing it. This was the situation I found myself in earlier this week.

To Unit Test routes in this scenario, it’s required to add an App.config to the project, and pull the routes into an external config file, calling this file from the App.config using configSource.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s