I'm trying to create unit tests to make sure my extension methods for UrlHelper work? Does anyone know how to do this? I'm using MVC 1.0 and MvcContrib. I can test the routes but can't test code like this:
public static string MoreFloorplans(this UrlHelper urlHelper, long productID, int pageIndex)
{
return urlHelper.Action<CatalogController>(x => x.GetRelatedProducts(productID, pageIndex));
}
In order to create a
UrlHelper
, you need aRequestContext
. In order to create a functioningRequestContext
, you need anHttpContextBase
and aRouteData
. The second,RouteData
, should be straightforward to construct. TheHttpContextBase
, you have to mock.For that, I'd suggest you look at Scott H's MvcMockHelpers. Parts of that are a little old, but I think it's good enough for this particular test - all you really need is the
FakeHttpContext
method and its dependencies. If you go pick up that library, your code would look something like:I know you say you're trying to use the MvcContrib
TestHelper
project, but as far as I know, that library is all about testing controllers. I'm not sure if it's really granular enough to test a lower-level component. You don't really need all the stuff in there anyway; all you need is aRequestContext
.