Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC Controller Unit Testing - Problem with UrlHelper Extension

Trying to do some controller unit-testing in my ASP.NET MVC 3 web application.

My test goes like this:

[TestMethod]
public void Ensure_CreateReviewHttpPostAction_RedirectsAppropriately()
{
   // Arrange.
   var newReview = CreateMockReview();

   // Act.
   var result = _controller.Create(newReview) as RedirectResult;

   // Assert.
   Assert.IsNotNull(result, "RedirectResult was not returned");
}

Pretty simple. Basically testing a [HttpPost] action to ensure it returns a RedirectResult (PRG pattern). I'm not using RedirectToRouteResult because none of the overloads support anchor links. Moving on.

Now, i'm using Moq to mock the Http Context, including server variables, controller context, session, etc. All going well so far.

Until i've hit this line in my action method:

return Redirect(Url.LandingPageWithAnchor(someObject.Uri, review.Uri);

LandingPageWithAnchor is a custom HTML helper:

public static string LandingPageWithAnchor(this UrlHelper helper, string uri1, string uri2)
{
   const string urlFormat = "{0}#{1}";

   return string.Format(urlFormat,
                helper.RouteUrl("Landing_Page", new { uri = uri1}),
                uri2);
}

Basically, i redirect to another page which is a "landing page" for new content, with an anchor on the new review. Cool.

Now, this method was failing before because UrlHelper was null.

So i did this in my mocking:

controller.Url = new UrlHelper(fakeRequestContext);

Which got it further, but now it's failing because the route tables don't contain a definition for "Landing_Page".

So i know i need to mock "something", but im not sure if it's:

a) The route tables
b) The UrlHelper.RouteUrl method
c) The UrlHelper.LandingPageWithAnchor extension method i wrote

Can anyone provide some guidance?

EDIT

This particular route is in an Area, so i tried calling the area registration in my unit test:

AreaRegistration.RegisterAllAreas();

But i get an InvalidOperationException:

This method cannot be called during the application's pre-start initialization stage.

like image 509
RPM1984 Avatar asked May 09 '11 23:05

RPM1984


1 Answers

Got it working by mocking the HttpContext, RequestContext and ControllerContext, registering the routes then creating a UrlHelper with those routes.

Goes a little like this:

public static void SetFakeControllerContext(this Controller controller, HttpContextBase httpContextBase)
{
    var httpContext = httpContextBase ?? FakeHttpContext().Object;
    var requestContext = new RequestContext(httpContext, new RouteData());
    var controllerContext = new ControllerContext(requestContext, controller);
    MvcApplication.RegisterRoutes();
    controller.ControllerContext = controllerContext;
    controller.Url = new UrlHelper(requestContext, RouteTable.Routes);
}

FakeHttpContext() is a Moq helper which creates all the mock stuff, server variables, session, etc.

like image 154
RPM1984 Avatar answered Sep 21 '22 22:09

RPM1984