How do I test Htmlhelper, Ajaxhelper and Urlhelper methods?

Asked

Viewed 158 times

1

I wrote some extensions for the MVC helpers. The problem is that I don’t know how to test these methods since I don’t have these objects in a test unit.

How do I test the methods I created?

1 answer

1


To get these helpers you need to instantiate them. One practical way is to have a class with static properties, so you can quickly get a helper in the context of your test.

public static class Helpers
{
    internal static AjaxHelper<dynamic> AjaxHelper
    {
        get
        {
            return new AjaxHelper<dynamic>(
                new ViewContext { HttpContext = new FakeHttpContext() },
                new FakeViewDataContainer()
            );
        }
    }

    internal static HtmlHelper<dynamic> HtmlHelper
    {
        get
        {
            return new HtmlHelper<dynamic>(
                new ViewContext { HttpContext = new FakeHttpContext() },
                new FakeViewDataContainer()
            );
        }
    }

    internal static UrlHelper UrlHelper
    {
        get
        {
            return new FakeUrlHelper(
                new RequestContext(new FakeHttpContext(), new RouteData())
            );
        }
    }
}

Note that I am creating the above objects with "false" classes, in English called mocks. Because there is no HTTP context in a unit test you need to create a false context.

To implement these mocks simply inherit from the basic classes:

Httpcontextbase:

internal class FakeHttpContext : HttpContextBase
{
    // exemplo de implementação de Items, note o override
    private Dictionary<object, object> _items = new Dictionary<object, object>();
    public override IDictionary Items { get { return _items; } }

    // caso você precise acessar o Request, você precisa de um mock para ele
    public override HttpRequestBase Request { get { return new FakeHttpRequest(); } }

    public override object GetService(Type serviceType)
    {
        return null;
    }
}

Httprequestbase:

internal class FakeHttpRequest : HttpRequestBase
{
    public override Uri Url { get { return new Uri("http://www.exemplo.com/"); } }

    public override string MapPath(string virtualPath)
    {
        return System.IO.Path.Combine(@"C:\temp", virtualPath.Replace("~/", "").Replace("/", "\\"));
    }

    public override string ApplicationPath { get { return "/"; } }
}

Iviewdatacontainer:

internal class FakeViewDataContainer : IViewDataContainer
{
    private ViewDataDictionary _viewData = new ViewDataDictionary();
    public ViewDataDictionary ViewData {
        get { return _viewData; }
        set { _viewData = value; } 
    }
}

With the mocks above you can implement a test and use things like:

Helpers.UrlHelper.RequestContext.HttpContext.Request.MapPath("~/arquivo.js");
Helpers.UrlHelper.Content("");

Helpers.HtmlHelper.ViewContext.HttpContext.Items.Add("x", 1);

Helpers.AjaxHelper.ActionLink("Link", "Action", "Controller");

And if you need more methods just do overrides.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.