开发者

How to test HtmlHelpers that call Partial?

开发者 https://www.devze.com 2023-04-08 21:56 出处:网络
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

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.Exis开发者_开发知识库tingRateDetailAR != 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 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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号