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

发布于 2024-10-21 08:41:42 字数 2240 浏览 3 评论 0原文

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

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 :值不能为空。 参数名称: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.

它似乎在以下中收到 ~ 的错误:

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

I am using ASP.NET MVC 3 and NUnit. I am trying to write a unit to test one of my helper methods. Here it 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 testing it the correct way? When I run it in the NUnit GUI then I get the following error:

System.ArgumentNullException : Value cannot be null.
Parameter name: httpContext

Is this possible to test? If so please explain in clear how do I get an instance of httpContext?

UPDATED

I can't get this test to pass. 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 that I wrote for it is the following:

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 NUnit GUI gives the following error:

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

It seems to be getting the error with the ~ in:

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蓝天白云 2024-10-28 08:41:42

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

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

如果您不想使用模拟框架,可以创建一个将从 HttpContextBase 派生并使用它的类。但这需要实现大量抽象成员,您可以通过模拟它来避免这种情况。

You need to mock HttpContext. Here's the example using Moq:

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

If you do not want to use a mocking framework, you can create a class that will derive from HttpContextBase and use it instead. But this will require implementing a lot of abstract members, which you can avoid by mocking it.

十二 2024-10-28 08:41:42

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

Personally I like using 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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文