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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:24:16+00:00 2026-06-01T04:24:16+00:00

Okay so I am using MVCContrib TestHelper to unit test my controllers, which works

  • 0

Okay so I am using MVCContrib TestHelper to unit test my controllers, which works great.

Like so many people though, by unit test I really mean integration test here and I want to at least make sure my views render without error given the model provided…otherwise I can miss a whole class of bugs basically related to the model even though I am testing the controller (like the view not rendering if a model property is null).

Anyway I started trying to figure out how to do this (aka googling how to do it). It seemed like the easiest way was to construct an HTMLHelper and have it just render the views (partial in this case).

Unfortunately when I try to use my mocked HTMLHelper it complains that is doesn’t have the controller name available in the route data.

Sure enough, I look and the controllers RouteData is not populated. Unfortunately the RouteData.Values RouteValueDictionary is read only, so I can’t just supply the necessary values.

I’m not married to the HTMLHelper idea to solve the problem of actually rendering the view as part of the test, so feel free to suggest alternatives there, but please don’t bother suggesting I test my Views using Selenium, Watin or other UI testing tools…I want the control to be able to do things like manipulate and restore state and data info for some of the tests, which I cannot do with UI based testing.

Here is the code I am currently using to try to render the partial:

    public class FakeView : IView
{
    #region IView Members
    public void Render(ViewContext viewContext, System.IO.TextWriter writer)
    {
        throw new NotImplementedException();
    }
    #endregion
}

public class WebTestUtilities
{
    public static void prepareCache()
    {
        SeedDataManager seed = new SeedDataManager();
        seed.CheckSeedDataStatus();
    }

    public static string RenderRazorViewToString(string viewName, object model, Controller controller)
    {
        var sb = new StringBuilder();
        var memWriter = new StringWriter(sb);
        var html = new HtmlHelper(new ViewContext(controller.ControllerContext,
            new FakeView(), new ViewDataDictionary(), new TempDataDictionary(), memWriter),
            new ViewPage());
        //This fails because it can't extract route information like the controller name)
        html.RenderPartial(viewName, model);
        return sb.ToString();
    }


    public void setupTestEnvironment(Controller controller)
    {
        RouteTable.Routes.Clear();
        RouteTable.Routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        RouteTable.Routes.MapRoute(
            "Default", 
            "{controller}/{action}/{id}", 
            new { controller = "Main", action = "DefaultView", id = UrlParameter.Optional } 
        );




        ModelBinders.Binders[typeof(DateTime)] = new DateTimeModelBinder();
        ModelBinders.Binders[typeof(DateTime?)] = new DateTimeModelBinder();
        ModelMetadataProviders.Current = new DateTimeMetadataProvider();



    }
}

And here is my test method:

        [TestMethod]
    public void GetUserTableView()
    {
        ViewResult result = controller.UserTable() as ViewResult;

        //I can populate the route and handler on the controller...
        RouteData routes = RouteTable.Routes.GetRouteData(controller.HttpContext);
        controller.RouteData.Route = routes.Route;
        controller.RouteData.RouteHandler = routes.RouteHandler;
        RouteValueDictionary routeKeys = new RouteValueDictionary();
        routeKeys.Add("controller", "UserManagement");
        routeKeys.Add("action", "UserTable");
        //But the RouteData.Values collection is read only :(
        controller.RouteData = new RouteData(){Values = routeKeys};
        string renderedView = WebTestUtilities.RenderRazorViewToString(result.ViewName, result.Model, controller);
    }

BTW, the specific error I get is :
The RouteData must contain an item named ‘controller’ with a non-empty string value.

  • 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-06-01T04:24:17+00:00Added an answer on June 1, 2026 at 4:24 am

    You may have already worked this out, but I just had a similar problem and solved setting the RouteData by resetting the ControllerContext on the controller like so:

    RouteData routeData = new RouteData();
    routeData.Values.Add("someRouteDataProperty", "someValue");
    ControllerContext controllerContext = new ControllerContext { RouteData = routeData };
    controller.ControllerContext = controllerContext;
    

    and then in the controller RouteData.Values.ContainsKey(“someRouteDataProperty”) works as you’ve set up in the test.

    This works because there’s a parameterless constructor of the ControllerContext that’s deliberately there to allow mocking etc.

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

Sidebar

Related Questions

Okay, so I've successfully completed my goal using buttons which was merely a test.
Okay so I'm not using any session variables, rather my code looks like this:
Okay, I have a function which reads a xml file and creates controls using
Is okay to break all dependencies using interfaces just to make a class testable?
Okay, so I'm doing my first foray into using the ADO.NET Entity Framework. My
Okay, so I am basically making a script to pass post data using cURL.
Okay, so I'm designing a stand-alone web service (using RestLET as my framework). My
Okay, so I've got the following code shown below to create a dialog using
Okay, so i have the following html added to a site using javascript/greasemonkey. (just
I'm using the below code for a rollover effect, seems to be working okay!

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.