How unit test urlHelper helper special method

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()
{
   // Arrange
   RequestContext requestContext = new RequestContext();
   UrlHelper urlHelper = new UrlHelper(requestContext);

   // Act
   string actual = urlHelper.YuiResetFontsGridsStylesheet();

   // Assert
   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()
{
   // Act
   string actual = urlHelper.Stylesheet();

   // Assert
   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/";
+3
source share
2

HttpContext. , Moq:

// Arrange
   var context = new Mock<HttpContextBase>();
   RequestContext requestContext = new RequestContext(context.Object, new RouteData());
   UrlHelper urlHelper = new UrlHelper(requestContext);

, , HttpContextBase . , , .

+8

MVCContrib TestHelper:

// arrange
// TODO: You could move this part in the SetUp part of your unit test
// to avoid repeating it in all tests
var cb = new TestControllerBuilder();
cb
    .HttpContext
    .Response
    .Stub(x => x.ApplyAppPathModifier(Arg<string>.Is.Anything))
    .WhenCalled(mi =>
    {
        mi.ReturnValue = mi.Arguments[0];
    })
    .Return(null);
var rc = new RequestContext(cb.HttpContext, new RouteData());
var helper = new UrlHelper(rc);

// act
var actual = helper.Stylesheet();

// assert
Assert.AreEqual("/Assets/Stylesheets/MyStylesheet.css", actual);
+2

All Articles