Unit Testing ASP.NET MVC Routes
Two things I always encourage customers to do are;
- Always delete the default route pattern after a File –> New project. This helps you focus on designing your routes throughout your site, and helps reduce SEO-unfriendly routes etc.
- Always unit test your routing table.
For number two, this is because it is so easy to have a route that looks like this;
/products/{productname}/{action}
… and then a developer comes along and adds a new route higher up the routing table that looks like this;
/products/{category}/{subcategory}
Hey presto, your website is broken. No-one can navigate to the {productname} pattern (assuming you have no other route validation going on). For this reason it is essential to unit test your routing table for any website, especially if it’s already live and you’re modifying it and adding functionality over time, because you could easily take down a whole piece of functionality. Remember to apply all the routes from every Area in your application during testing.
Rather than write up how to go about doing this unit testing, let me point you at some great resources;
- Unit Testing ASP.NET MVC (MSDN Magazine) – scroll down a little to “Testing Routes”
- Testing Routing and URL generation (Brad Wilson)
- Unit Test your Custom Routes (Stephen Walther – VB.NET code)
- Testing Routes in MVC and RouteEvaluator for Unit Testing Routes (Phil Haack)
Have a read, and write those tests today
Comments
- Anonymous
July 20, 2012
I've written a small blog avout I would appreciate some feedback from you gigawebsolution.com/.../Unit-test-Custom-Route-in-MVC