I use ASP.NET MVC 3and NUnit. I am trying to write a block to test one of my helper methods. There he is:
public static class UrlHelperAssetExtensions
{
private static readonly string yuiBuildPath = "http://yui.yahooapis.com/2.8.2r1/build/";
public static string YuiResetFontsGridsStylesheet(this UrlHelper helper)
{
return helper.Content(yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css");
}
}
Here is my unit test:
[Test]
public void YuiResetFontsGridsStylesheet_should_return_stylesheet()
{
RequestContext requestContext = new RequestContext();
UrlHelper urlHelper = new UrlHelper(requestContext);
string actual = urlHelper.YuiResetFontsGridsStylesheet();
string expected = yuiBuildPath + "reset-fonts-grids/reset-fonts-grids.css";
Assert.AreEqual(expected, actual);
}
Am I checking this correctly? When I run it in the NUnit GUI, I get the following error:
System.ArgumentNullException: value cannot be null. Parameter Name: httpContext
Can this be verified? If yes, explain how can I get an instance of httpContext?
UPDATED
I can not pass this test. In my method, I have the following:
private static readonly string stylesheetPath = "~/Assets/Stylesheets/";
public static string Stylesheet(this UrlHelper helper)
{
return helper.Content(stylesheetPath + "MyStylesheet.css");
}
The test I wrote for him is as follows:
private string stylesheetPath = "/Assets/Stylesheets/";
private HttpContextBase httpContextBaseStub;
private RequestContext requestContext;
private UrlHelper urlHelper;
[SetUp]
public void SetUp()
{
httpContextBaseStub = MockRepository.GenerateStub<HttpContextBase>();
requestContext = new RequestContext(httpContextBaseStub, new RouteData());
urlHelper = new UrlHelper(requestContext);
}
[Test]
public void Stylesheet_should_return_stylesheet()
{
string actual = urlHelper.Stylesheet();
string expected = stylesheetPath + "MyStylesheet.css";
Assert.AreEqual(expected, actual);
}
The following error appears in the NUnit GUI:
System.NullReferenceException : Object reference not set to an instance of an object.
It seems to get an error with ~ in:
private static readonly string stylesheetPath = "~/Assets/Stylesheets/";