SparkyTestHelpers: ASP.NET MVC Routing

part of the “SparkyTestHelpers” suite of .NET unit testing tools

photo by Summer

NuGet package | Source code

This is the last planned update to SparkyTestHelpers for a while. I’ve finished packaging all of the /NET unit test helpers that I’ve accumulated over the years. I start a new job tomorrow, so as I encounter new areas of the .NET universe that I haven’t worked with before, I’m sure I’ll be developing some new tools to make testing easier.

I’ve updated the .NET Framework version of SparkyTestHelpers.AspNetMvc to add helpers for testing routing. These helpers:

  • are for testing code-specified routes (via RouteConfig.RegisterRoutes or an AreaRegistration implementation) only. They do not provide the ability to test attribute-based routing.
  • are not currently included in the SparkyTestHelpers.AspNetMvc.Core NuGet package. Routing has been rewritten for .NET Core, and I’ll have to figure out how to mock a bunch of different dependencies before I can add routing test helpers for .NET Core. It looks like Ivaylo Kenov has figured it out, so if you want to unit test ASP.NET Core routing now, I suggest you look at his GitHub repo.

RouteTester

RouteTester and RoutingAsserter provide methods to assert that a given relative URL maps to the expected RouteData.Values. The RoutingAsserter.AssertMapTo overloads provide multiple ways to specify the expected values…

Constructors

  • public RouteTester(Action<RouteCollection> routeRegistrationMethod)
  • public RouteTester(AreaRegistration areaRegistration)
using SparkyTestHelpers.AspNetMvc.Routing;
. . .
  var routeTester = new RouteTester(RouteConfig.RegisterRoutes);
  var areaRouteTester = new RouteTester(new FooAreaRegistration());

methods

  • .ForUrl(string relativeUrl) — creates a new RoutingAsserter instance.

RoutingAsserter

methods

  • .AssertMapTo(IDictionary<string, object> expectedValues)
  • .AssertMapTo(object routeValues)
  • .AssertMapTo(string controller, string action, (object id)) — id defaults to null
  • .AssertMapTo<TController>(Expression<Func<TController, Func<ActionResult>>> actionExpression)
  • .AssertRedirectTo(string expectedUrl, (HttpStatusCode *expectedStatusCode)) — expectedStatusCode defaults to HttpStatusCode.Redirect (302)

examples

routeTester.ForUrl("Default.aspx)
.AssertRedirectTo("Home/LegacyRedirect");
// alternate syntaxes for asserting Home/Index routing:
routeTester.ForUrl("Home/Index")
.AssertMapTo(new Dictionary<string, object>
{{ "controller", "Home" }, { "action", "Index" }, { "id", null });
routeTester.ForUrl("Home/Index")
.AssertMapTo(new {controller = "Home", action = "Index"});
routeTester.ForUrl("Home/Index")
.AssertMapTo("Home", "Index");
routeTester.ForUrl("Home/Index")
.AssertMapTo<HomeController>(x => x.Index);
// alternate syntaxes for asserting Order/Details/3 routing:
routeTester.ForUrl("Order/Details/3")
.AssertMapTo(new Dictionary<string, object>
{{"controller", "Order"}, {"action", "Details"}, {"id", 3});
routeTester.ForUrl("Order/Details/3")
.AssertMapTo(
new {controller = "Order", action = "Details", id = 3 });
routeTester.ForUrl("Order/Details/3")
.AssertMapTo("Order", "Details", 3);
routeTester.ForUrl("Order/Details/3")
.AssertMapTo<OrderController>(x => () => x.Details(3));

Happy Testing!