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 490621
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T01:55:41+00:00 2026-05-13T01:55:41+00:00

I am writing unit tests against my ASP.NET MVC application, in particular I am

  • 0

I am writing unit tests against my ASP.NET MVC application, in particular I am testing an HtmlHelper extension method that I wrote. There is a line inside of the extension method:

var innerHtml = htmlHelper.ActionLink(text, action, controller, routeValues, null);

When I run this inside of my unit test, the href of the generated URL is blank regardless of the action or controller passed in.

Here is my unit test:

var page = CreateProductDataPage(); //returns ProductDataPage object
var htmlHelper = Http.CreateHtmlHelperWithMocks<ProductDataPage>(new ViewDataDictionary<ProductDataPage>(page), false);
var result = htmlHelper.ProductListingBreadcrumb(true, null, null);

Here is the CreateHtmlHelperWithMocks method:

public static HtmlHelper<T> CreateHtmlHelperWithMocks<T>(ViewDataDictionary<T> viewData, bool isLoggedIn) where T : class
{
    var mockViewDataContainer = new Mock<IViewDataContainer>();
    mockViewDataContainer.SetupGet(v => v.ViewData).Returns(viewData);

    return new HtmlHelper<T>(GetViewContextMock(viewData, isLoggedIn).Object, mockViewDataContainer.Object);
}

Finally, here is the GetViewContextMock method:

public static Mock<ViewContext> GetViewContextMock(ViewDataDictionary viewData, bool isLoggedIn)
{
    var mock = new Mock<ViewContext>();

    mock.SetupGet(v => v.HttpContext).Returns(GetHttpContextMock(isLoggedIn).Object);
    mock.SetupGet(v => v.Controller).Returns(new Mock<ControllerBase>().Object);
    mock.SetupGet(v => v.View).Returns(new Mock<IView>().Object);
    mock.SetupGet(v => v.ViewData).Returns(viewData);
    mock.SetupGet(v => v.TempData).Returns(new TempDataDictionary());
    mock.SetupGet(v => v.RouteData).Returns(new RouteData());

    return mock;
}
  • 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-13T01:55:41+00:00Added an answer on May 13, 2026 at 1:55 am

    Update: Figured it out. What a pain in the a$$. In case anyone else tries to do this…

    The first step was to add the Route Collection from the global.asax in the creation of the mock HtmlHelper.

        public static HtmlHelper<T> CreateHtmlHelperWithMocks<T>(ViewDataDictionary<T> viewData, bool isLoggedIn) where T : class
        {
            var mockViewDataContainer = new Mock<IViewDataContainer>();
            mockViewDataContainer.SetupGet(v => v.ViewData).Returns(viewData);
    
            //These next two lines are key:
            var routeCollection = new RouteCollection();
            MvcApplication.RegisterRoutes(routeCollection);
    
            return new HtmlHelper<T>(GetViewContextMock(viewData, isLoggedIn).Object, mockViewDataContainer.Object, routeCollection);
        }
    

    Then I had to make sure the HttpContext mock had a result being returned for the Request’s ApplicationPath property and the Response’s ApplyAppPathModifier method.

        public static Mock<HttpContextBase> GetHttpContextMock(bool isLoggedIn)
        {
            var context = new Mock<HttpContextBase>();
            var request = new Mock<HttpRequestBase>();
            var response = new Mock<HttpResponseBase>();
            var session = new Mock<HttpSessionStateBase>();
            var server = new Mock<HttpServerUtilityBase>();
            var principal = AuthenticationAndAuthorization.GetPrincipleMock(isLoggedIn);
    
            //These next two lines are required for the routing to generate valid URLs, apparently:
            request.SetupGet(r => r.ApplicationPath).Returns("/");
            response.Setup(r => r.ApplyAppPathModifier(It.IsAny<string>())).Returns((string r) => r);
    
            context.SetupGet(c => c.Request).Returns(request.Object);
            context.SetupGet(c => c.Response).Returns(response.Object);
            context.SetupGet(c => c.Session).Returns(session.Object);
            context.SetupGet(c => c.Server).Returns(server.Object);
            context.SetupGet(c => c.User).Returns(principal.Object);
    
            return context;
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We are writing unit tests for our ASP.NET application that run against a test
I just got started running UI tests against my ASP.NET MVC application using WatiN.
I am writing unit tests for a spring mvc 3.1 application that is not
I have a PersonDao that I'm writing unit tests against. There are about 18-20
I'm writing unit tests with NUnit and the TestDriven.NET plugin. I'd like to provide
More precisely does writing unit tests consider as BDD practice? Does unite testing consider
In writing unit tests for Django, how would I test whether a particular view
I'm writing some Unit tests against a database, and we're using transactions to make
I'm writing unit-tests for an app that uses a database, and I'd like to
Iam writing unit tests for my repository and I need to test repository method

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.