如何对 UrlHelper 自定义帮助器方法进行单元测试
我正在使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您需要模拟 HttpContext。以下是使用 Moq 的示例:
如果您不想使用模拟框架,可以创建一个将从 HttpContextBase 派生并使用它的类。但这需要实现大量抽象成员,您可以通过模拟它来避免这种情况。
You need to mock HttpContext. Here's the example using Moq:
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.
就我个人而言,我喜欢使用 MVCContrib TestHelper:
Personally I like using MVCContrib TestHelper: