Of Code and Me

Somewhere to write down all the stuff I'm going to forget and then need

Create an Asp.Net MVC HtmlHelper for use in unit tests November 17, 2009

Filed under: Asp.Net,C#,Coding,MVC,Web — Rupert Bates @ 5:47 pm

Here’s a utility factory class to create an HtmlHelper instance so that you can unit test extension methods written on it. It will take a controller and a model for methods which rely on those. I adapted it from this post, and changed it to work with Asp.Net version 1.0 and to accept a model.

using System;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using KableDirect.Web.FrontEnd.Controllers;

namespace KableDirect.UnitTests
{
    class HtmlHelperFactory
    {
        public static HtmlHelper CreateInstance(RouteData route)
        {
            return CreateInstance(route, new HomeController());
        }
        public static HtmlHelper CreateInstance(RouteData route, Controller controller)
        {
            return CreateInstance(route, controller, null);
        }
        public static HtmlHelper CreateInstance(RouteData route, Controller controller, object model)
        {
            HttpContextBase httpContext = new HttpContextDummy();

            var cc = new ControllerContext(httpContext, route, controller);
            var vd = new ViewDataDictionary(model);
            ViewContext vc = new ViewContext(cc, new ViewDummy(), vd, new TempDataDictionary());
            return new HtmlHelper(vc, new ViewDataContainerDummy(vd), new RouteCollection());
        }


        // Dummy classes needed to be able to create HtmlHelper

        private class HttpRequestDummy : HttpRequestBase
        {
            public override string ApplicationPath
            {
                get { return ""; }
            }

            public override string AppRelativeCurrentExecutionFilePath
            {
                // Any shorter string here gives exception:
                // index larger than length of string
                get { return "~/"; }
            }

            public override string PathInfo
            {
                get { return ""; }
            }
        }

        private class HttpResponseDummy : HttpResponseBase
        {
            public override string ApplyAppPathModifier(string virtualPath)
            {
                return virtualPath;
            }
        }

        private class HttpContextDummy : HttpContextBase
        {
            public override HttpRequestBase Request
            {
                get { return new HttpRequestDummy(); }
            }

            public override HttpResponseBase Response
            {
                get { return new HttpResponseDummy(); }
            }
        }

        private class ViewDummy : IView
        {
            public void Render(ViewContext viewContext, System.IO.TextWriter writer)
            {
                throw new NotImplementedException();
            }
        }

        private class ViewDataContainerDummy : IViewDataContainer
        {
            public ViewDataContainerDummy()
            {
            }

            public ViewDataContainerDummy(ViewDataDictionary dataDictionary)
            {
                _data = dataDictionary;
            }

            private ViewDataDictionary _data;
            public ViewDataDictionary ViewData
            {
                get { return _data; }
                set { _data = value; }
            }
        }
    }
}

 

Advertisements
 

One Response to “Create an Asp.Net MVC HtmlHelper for use in unit tests”

  1. […] to VoteCreate an Asp.Net MVC HtmlHelper for use in unit tests (11/17/2009)Tuesday, November 17, 2009 from eliasbland.wordpress.comI adapted it from this post, and changed it […]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s