1

I've created a custom helper according to instructions here and here. Here's a snippet of what it looks like (ThemeHelper.cs):

@inherits Helpers.HelperPage
@using System.Web.Mvc
@using System.Web.Mvc.Html
@...

@helper PathTo(string fileName) {
    @Url.Content("~/Content/Themes/" + Theme.CurrentTheme.FolderName + "/" + fileName);
}

I've placed this in App_Code, as instructed. I can use these helpers in my views, which is what I want.

Now my question is, how do I test this thing? I cannot, for example, reflectively get an instance of the ThemeHelper class, neither in the current assembly nor by reflectively accessing the App_Code or __Code assemblies (neither of which actually return).

Ideally, I would like to somehow call these functions and verify the results/HTML. I have a framework in place (C# version of HtmlUnit 2.7) that allows me to request URLs and verify the HTML.

Is there a way to test my custom helper? I would like to write something like:

ThemeHelper h = new ThemeHelper(); // or: Assembly.CreateInstance(...) or something
string html = h.PathTo("Site.css");
Assert.IsTrue(html.contains("Themes");
Community
  • 1
  • 1
ashes999
  • 9,925
  • 16
  • 73
  • 124

1 Answers1

0

Now my question is, how do I test this thing?

It won't be easy to unit test Razor Page helpers in isolation.

In order to have unit testable and view agnostic helpers (not dependent on the specific view engine you are using) you should use standard HTML Helpers that come as an extension method to the HtmlHelper or UrlHelper classes. Example:

public static class UrlExtensions
{
    public static string ThemeHelper(this UrlHelper urlHelper, string fileName)
    {
        return urlHelper.Content("~/Content/Themes/" + Theme.CurrentTheme.FolderName + "/" + fileName);
    }
}
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
  • The problem is that my view helper is something other developers will use to make their life easier when creating custom views (think Wordpress style theme development). I can't ditch it for a normal class without losing the ease of syntax, or can I? – ashes999 Nov 22 '11 at 17:19
  • @ashes999, there is indeed the right balance to find between the 2. Html helpers should be used for generating short fragments of dynamic HTML code. They can be unit tested. Personally I never use Razor helpers. I prefer using Editor/Display templates in which I call my Html helpers defined in classes. – Darin Dimitrov Nov 22 '11 at 17:22
  • @DarianDimitrov the problem is that as an API developer, I don't want to intermix calls into the HtmlHelper and other helpers; I want to provide a separate class so users can see the API at a glance. – ashes999 Nov 22 '11 at 20:31
  • You could define the class containing your custom helper methods in the `System.Web.Mvc.Html` namespace. This way all your users have to do is to reference the assembly containing your helpers and they will immediately *see* those helpers in the views without the need of bringing a custom namespace into scope. – Darin Dimitrov Nov 22 '11 at 21:24
  • Okay, cool. Can you provide some sample code? I don't see how to unit test it if it's an extension method to UrlHelper or HtmlHelper. – ashes999 Nov 23 '11 at 00:09