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");
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
orUrlHelper
classes. Example: