In order to create a UrlHelper
, you need a RequestContext
. In order to create a functioning RequestContext
, you need an HttpContextBase
and a RouteData
. The second, RouteData
, should be straightforward to construct. The HttpContextBase
, 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:
[TestMethod]
public void Can_write_more_floorplans()
{
const long productID = 12345;
const int pageIndex = 10;
var httpContext = FakeHttpContext(); // From the MvcMockHelpers
var routeData = new RouteData();
var requestContext = new RequestContext(httpContext, routeData);
var urlHelper = new UrlHelper(requestContext);
string floorplans = MoreFloorplans(urlHelper, productID, pageIndex);
Assert.AreEqual(some_string, floorplans);
}
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 a RequestContext
.