Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6356579
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T22:59:25+00:00 2026-05-24T22:59:25+00:00

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

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T22:59:27+00:00Added an answer on May 24, 2026 at 10:59 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on an ASP.NET MVC application, and am trying to write some unit
We are trying to write Unit Tests in our ASP.Net MVC project. Some of
I'm trying some of the ASP.NET MVC tutorials and one of them has the
I've started with ASP.NET MVC recently, reading blogs, tutorials, trying some routes, etc. Now,
I am currently trying to write some unit test against my zend framework controller.
I'm building a small application in ASP.NET MVC. I'm still trying to find my
I am trying to get my feet wet with asp.net mvc3 and unit testing.
I'm trying to get my head around how one would unit test an ASP.NET
I have an ASP.NET MVC web application running from http://localhost/myappname . From jQuery, I
I am working through the book Professional ASP.NET MVC 2 and I am trying

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.