如何测试调用 Partial 的 HtmlHelpers?

发布于 2024-12-07 05:28:25 字数 1522 浏览 1 评论 0原文

我一直在查看这个 Stackoverflow 问题并已实现答案。它工作得很好,直到我在我的帮助方法中调用 HtmlHelper.Partial,如下所示。我知道这可能不是最好的代码,但这是在我可以重构更多应用程序之前。它抛出的错误是

上一个方法“ViewContext.get_TempData();”需要返回值或抛出异常。

我是否错过了模拟某些东西,或者是否有更好的方法来呈现用户控件?

编辑 好吧,我确实错过了一些东西,我没有调用mocks.Replay()。现在有另一个错误,它需要在routeData...progress 中命名名为controller 的东西。

编辑#2澄清我正在尝试模拟对 HtmlHelper.Partial(partialPath, model) 的调用,我只是希望它返回我认为发送的任何partialPath,或者至少不爆炸。我确实找到了此页面http://andrevianna.com/blog/?p=8这非常有帮助,我几乎让事情顺利进行。这也很有帮助 http://farm -fresh-code.blogspot.com/2009/10/mocking-htmlhelper-class-with.html

 public static string RenderRateDetails(this HtmlHelper html, string partialPath, RatesViewData model, RateDetailType type)
    {

        switch (type)
        {
            case RateDetailType.AR:
                if (model.ExistingRateDetailAR != null)
                    return html.Partial(partialPath, model).ToString();
                break;
            case RateDetailType.AP:
                if (model.ExistingRateDetail != null)
                    return html.Partial(partialPath, model).ToString();
                break;

        }

        return string.Empty;
    }

I've been looking at this Stackoverflow question and have the answer implemented. It works all fine and dandy until I get to call HtmlHelper.Partial in my helper method, which is listed below. I know it might not be the best code, but this is until I can refactor more of the app. The error it throws is

Previous method 'ViewContext.get_TempData();' requires a return value or an exception to throw.

Am I missing mocking something, or is there a better way to render a usercontrol?

Edit Ok I did miss something, I didn't call mocks.Replay(). Now have another error which it wants something named controller in routeData...progress.

Edit #2 Clarifying I'm trying to mock the call to HtmlHelper.Partial(partialPath, model), I just want that to return whatever partialPath I send in I suppose, or at least not blowup. I did find this page http://andrevianna.com/blog/?p=8 which was very helpful and I almost got things working. This was helpful as well http://farm-fresh-code.blogspot.com/2009/10/mocking-htmlhelper-class-with.html

 public static string RenderRateDetails(this HtmlHelper html, string partialPath, RatesViewData model, RateDetailType type)
    {

        switch (type)
        {
            case RateDetailType.AR:
                if (model.ExistingRateDetailAR != null)
                    return html.Partial(partialPath, model).ToString();
                break;
            case RateDetailType.AP:
                if (model.ExistingRateDetail != null)
                    return html.Partial(partialPath, model).ToString();
                break;

        }

        return string.Empty;
    }

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

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

发布评论

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

评论(2

指尖上的星空 2024-12-14 05:28:26

我认为“农场新鲜代码”中给出的示例是正确的方法,您不能直接模拟 HtmlHelper,但您可以构建一个实例,其中所有依赖项都被模拟。

当您的代码调用 html.Partial(partialPath, model).ToString() 时,HtmlHelper 会调用您模拟的依赖项上的属性和方法,如果这些不返回合理的值,您会收到错误默认值。

在这种情况下,看起来模拟的 ViewContext 对象的 TemplateData 属性被调用,我想它返回 null,因此:

Previous method 'ViewContext.get_TempData();' requires a return value or an exception to throw.

一旦您模拟此属性,您应该能够克服此错误,但您可能需要模拟一些在一切正常工作之前还有更多事情要做。

查看 MVC 源代码以了解 Partial 方法中调用了什么内容可能会节省您一些时间。您可以在这里获取 http://aspnet.codeplex.com/releases/view/58781

编辑

顺便说一句。 TempData 属性返回 System.Web.Mvc.TempDataDictionary。模拟该属性以返回其中之一的空实例应该可以解决眼前的问题。

I think the example given at 'farm fresh code' is the right way to go, you can't directly mock the HtmlHelper, but you can build an instance where all of it's dependencies are mocked.

When you're code calls html.Partial(partialPath, model).ToString(), the HtmlHelper calls properties and methods on the dependencies that you mocked, and you get errors if these don't return reasonable default values.

In this case it looks like the TemplateData property of the mocked ViewContext object was called, and I imagine it returned null, hence:

Previous method 'ViewContext.get_TempData();' requires a return value or an exception to throw.

Once you mock this property, you should be able to get past this error, but you might need to mock a few more things before you get it all working.

It might save you some time to take a look at the MVC source code to see what gets called in the Partial method. You can get that here http://aspnet.codeplex.com/releases/view/58781.

EDIT

BTW. The TempData property returns a System.Web.Mvc.TempDataDictionary. Mocking the property to return an empty instance of one of those should solve the immediate problem.

檐上三寸雪 2024-12-14 05:28:26

您是否考虑过为您的用户控件使用显示和编辑器模板而不是扩展 HtmlHelper?

我曾经在早期的 MVC 版本中经常做同样的事情,但现在我几乎完全转向使用模板。

Have you considered using Display and Editor templates for your user controls rather than extending HtmlHelper?

I used to do the same thing quite a lot in the early MVC versions, but I have switched almost completely to using templates now.

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