Showing posts with label MVC Contrib. Show all posts
Showing posts with label MVC Contrib. Show all posts

Tuesday, September 7, 2010

How to test ASP.NET MVC routines with optional parameters

I defined a map route in Global.asax.cs as below
routes.MapRoute(null,
"product/{category}",
new { controller = "Product", action = "List", category = UrlParameter.Optional });
and ProductController's List method
public ActionResult List(string category = null)
{
IList<Product> products;

if (category == null)
{
products = _productRepository.GetAll();
}
else
{
products = _productRepository.GetAllByCategory(category);
}

return View(products);
}
And here are a few attempts to test the route (with MVC Contrib Test Helper support)

First attempt


"~/product".Route().ShouldMapTo<ProductController>(c => c.List());
I have a compile error: "An expression tree may not contain a call or invocation that uses optional arguments". Grrrr, expressions don't play with optional arguments.

Second attempt


"~/product".Route().ShouldMapTo<ProductController>(c => c.List(null));
The test throws a MvcContrib.TestHelper.AssertionException: "Value for parameter 'category' did not match: expected 'System.Web.Mvc.UrlParameter' but was ''."

Third attempt


"~/product".Route().ShouldMapTo<ProductController>(c => c.List(""));
Same result as second attempt. Grrrr...

And the forth attempt


var routeData = "~/product".WithMethod(HttpVerbs.Get);
routeData.Values["category"] = null;
routeData.ShouldMapTo<ProductController>(c => c.List(null));
Now the test pass.

Why? 'category' is an optional parameter, as mentioned here, I expect that I don't need to explicit to define it in route value dictionary and the third attempt should be passed. Is a place to improve MVC Contrib test helper?