3

I have a few UrlHelper extension methods that I'd like to unit test. However, I'm getting a NullReferenceException from the UrlHelper.Content(string) method when the path begins with "~/". Anyone know what the issue is?

[Test]
public void DummyTest()
{
    var context = new Mock<HttpContextBase>();
    RequestContext requestContext = new RequestContext(context.Object, new RouteData());
    UrlHelper urlHelper = new UrlHelper(requestContext);

    string path = urlHelper.Content("~/test.png");

    Assert.IsNotNullOrEmpty(path);
}
5
  • Can you show us the UrlHelper.Content() method? Commented Oct 3, 2012 at 0:42
  • seems like simple "file not found" in the relative to the assembly path. Commented Oct 3, 2012 at 0:43
  • @MikeParkhill The UrlHelper.Content method is defined in the System.Web.Mvc namespace. That's not one of my methods. Commented Oct 3, 2012 at 0:48
  • @trailmax That's not how UrlHelper.Content works. It converts a path like '~/test.png' into a path suitable to be rendered in an anchor or image tag. Commented Oct 3, 2012 at 0:49
  • 1
    @devlife, of course, you're right! (smacks forehead) Commented Oct 3, 2012 at 0:54

1 Answer 1

10

When you create the UrlHelper with the RouteContext the HttpContext is null in your unit test environment. Without it you'll hit a lot of NullReferenceExceptions when you try to call any methods that rely on it.

There are a number of threads out there on mocking the various web contexts. You can check out this one: How do I mock the HttpContext in ASP.NET MVC using Moq?

or this one Mock HttpContext.Current in Test Init Method

EDIT: The following will work. Note you need to mock the HttpContext.Request.ApplicationPath and the HttpContext.Response.ApplyAppPathModifier().

[Test]
public void DummyTest() {
    var context = new Mock<HttpContextBase>();
    context.Setup( c => c.Request.ApplicationPath ).Returns( "/tmp/testpath" );
    context.Setup( c => c.Response.ApplyAppPathModifier( It.IsAny<string>( ) ) ).Returns( "/mynewVirtualPath/" );
    RequestContext requestContext = new RequestContext( context.Object, new RouteData() );
    UrlHelper urlHelper = new UrlHelper( requestContext );

    string path = urlHelper.Content( "~/test.png" );

    Assert.IsNotNullOrEmpty( path );
}

I found the details for this in the following thread: Where does ASP.NET virtual path resolve the tilde "~"?

Sign up to request clarification or add additional context in comments.

2 Comments

I tried the suggestions from those links to no avail. It might be worth pointing out that the test passes if you remove the "~" from the path.
From the MSDN article: "If the specified content path does not start with the tilde (~) character, this method returns contentPath unchanged." So, if you take the tilde out it's not doing any actual work under the hood.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.