如何对UrlHelper自定义帮助器方法进行单元测试

时间:2011-03-10 12:33:17

标签: asp.net asp.net-mvc asp.net-mvc-2 asp.net-mvc-3 nunit

我正在使用ASP.NET MVC 3NUnit。我正在尝试编写一个单元来测试我的一个辅助方法。这是:

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");
   }
}

这是我的单元测试:

[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);
}

我是否以正确的方式测试?当我在NUnit GUI中运行它时,我收到以下错误:

System.ArgumentNullException:值不能为null。 参数名称:httpContext

这可以测试吗?如果是这样,请明确说明如何获取httpContext的实例?

已更新

我无法通过此测试。在我的方法中,我有以下内容:

private static readonly string stylesheetPath = "~/Assets/Stylesheets/";

public static string Stylesheet(this UrlHelper helper)
{
   return helper.Content(stylesheetPath + "MyStylesheet.css");
}

我为它写的测试如下:

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);
}

NUnit GUI出现以下错误:

System.NullReferenceException : Object reference not set to an instance of an object.

似乎是在~in:

中得到了错误
private static readonly string stylesheetPath = "~/Assets/Stylesheets/";

2 个答案:

答案 0 :(得分:9)

你需要模拟HttpContext。以下是使用Moq的示例:

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

如果您不想使用模拟框架,可以创建一个从HttpContextBase派生并使用它的类。但这需要实现许多抽象成员,你可以通过嘲笑来避免这些成员。

答案 1 :(得分:2)

我个人喜欢使用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);