单元测试 UrlHelper 扩展方法

发布于 2024-08-29 07:17:14 字数 339 浏览 12 评论 0原文

我正在尝试创建单元测试以确保我的 UrlHelper 扩展方法有效?有谁知道该怎么做?我正在使用 MVC 1.0 和 MvcContrib。我可以测试路线,但无法测试这样的代码:

    public static string MoreFloorplans(this UrlHelper urlHelper, long productID, int pageIndex)
    {
     return urlHelper.Action<CatalogController>(x => x.GetRelatedProducts(productID, pageIndex));

    }

I'm trying to create unit tests to make sure my extension methods for UrlHelper work? Does anyone know how to do this? I'm using MVC 1.0 and MvcContrib. I can test the routes but can't test code like this:

    public static string MoreFloorplans(this UrlHelper urlHelper, long productID, int pageIndex)
    {
     return urlHelper.Action<CatalogController>(x => x.GetRelatedProducts(productID, pageIndex));

    }

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

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

发布评论

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

评论(2

ゞ记忆︶ㄣ 2024-09-05 07:17:14

我按照 Aaronaught 和 Scott H 的指示进行操作,但还是花了一些功夫。我最终得到了这样的东西。

public UrlHelper GetUrlHelper(
        string fileName = "/",
        string url="http://localhost", 
        string queryString="")
{
    // Use routes from actual app
    var routeCollection = new RouteCollection();
    MvcApplication.RegisterRoutes(routeCollection);

    //Make a request context
    var request = new HttpRequest(fileName, url, queryString);
    var response = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(request, response);
    var httpContextBase = new HttpContextWrapper(httpContext);
    var requestContext = new RequestContext(httpContextBase, new RouteData());

    // Make the UrlHelper with empty route data
    return new UrlHelper(requestContext, routeCollection);
}

public void MoreFloorplans_ReturnsExpectedUrl()
{
    var urlHelper = GetUrlHelper();
    var actualResult = urlHelper.MoreFloorPlans(1,2);
    Assert.AreEqual("/MoreFloorPlans/1/2", actualResult);
}

请注意,您应该测试扩展方法,而不是 UrlHelper 本身,因此在 RequestContext 中设置 RouteData 可能超出范围。

I followed the instructions from Aaronaught and Scott H but it took some goofing around. I ended up with something like this.

public UrlHelper GetUrlHelper(
        string fileName = "/",
        string url="http://localhost", 
        string queryString="")
{
    // Use routes from actual app
    var routeCollection = new RouteCollection();
    MvcApplication.RegisterRoutes(routeCollection);

    //Make a request context
    var request = new HttpRequest(fileName, url, queryString);
    var response = new HttpResponse(new StringWriter());
    var httpContext = new HttpContext(request, response);
    var httpContextBase = new HttpContextWrapper(httpContext);
    var requestContext = new RequestContext(httpContextBase, new RouteData());

    // Make the UrlHelper with empty route data
    return new UrlHelper(requestContext, routeCollection);
}

public void MoreFloorplans_ReturnsExpectedUrl()
{
    var urlHelper = GetUrlHelper();
    var actualResult = urlHelper.MoreFloorPlans(1,2);
    Assert.AreEqual("/MoreFloorPlans/1/2", actualResult);
}

Note that you should be testing your extension method, not UrlHelper itself, so setting up the RouteData in the RequestContext is probably out of scope.

醉酒的小男人 2024-09-05 07:17:14

为了创建 UrlHelper,您需要一个 RequestContext。为了创建一个有效的 RequestContext,您需要一个 HttpContextBase 和一个 RouteData。第二个,RouteData,应该很容易构建。 HttpContextBase,您必须进行模拟。

为此,我建议您查看 Scott H 的 MvcMockHelpers。其中部分内容有点旧,但我认为对于这个特定的测试来说它已经足够好了 - 您真正需要的是 FakeHttpContext 方法及其依赖项。如果您选择该库,您的代码将类似于:

[TestMethod]
public void Can_write_more_floorplans()
{
    const long productID = 12345;
    const int pageIndex = 10;

    var httpContext = FakeHttpContext();  // From the MvcMockHelpers
    var routeData = new RouteData();
    var requestContext = new RequestContext(httpContext, routeData);
    var urlHelper = new UrlHelper(requestContext);
    string floorplans = MoreFloorplans(urlHelper, productID, pageIndex);
    Assert.AreEqual(some_string, floorplans);
}

我知道您说您正在尝试使用 MvcContrib TestHelper 项目,但据我所知,该库都是关于测试的控制器。我不确定它是否真的足够精细来测试较低级别的组件。无论如何,你并不真的需要那里的所有东西;您所需要的只是一个RequestContext

In order to create a UrlHelper, you need a RequestContext. In order to create a functioning RequestContext, you need an HttpContextBase and a RouteData. The second, RouteData, should be straightforward to construct. The HttpContextBase, you have to mock.

For that, I'd suggest you look at Scott H's MvcMockHelpers. Parts of that are a little old, but I think it's good enough for this particular test - all you really need is the FakeHttpContext method and its dependencies. If you go pick up that library, your code would look something like:

[TestMethod]
public void Can_write_more_floorplans()
{
    const long productID = 12345;
    const int pageIndex = 10;

    var httpContext = FakeHttpContext();  // From the MvcMockHelpers
    var routeData = new RouteData();
    var requestContext = new RequestContext(httpContext, routeData);
    var urlHelper = new UrlHelper(requestContext);
    string floorplans = MoreFloorplans(urlHelper, productID, pageIndex);
    Assert.AreEqual(some_string, floorplans);
}

I know you say you're trying to use the MvcContrib TestHelper project, but as far as I know, that library is all about testing controllers. I'm not sure if it's really granular enough to test a lower-level component. You don't really need all the stuff in there anyway; all you need is a RequestContext.

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