单元测试我控制器的方法会导致一个空VIEWNAME? [英] Unit testing my controller method results in an empty ViewName?

查看:143
本文介绍了单元测试我控制器的方法会导致一个空VIEWNAME?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做一些简单的MS单元测试我的标准,没有什么特别的控制器。

当我检查 VIEWNAME proprty,从返回的ViewResult 对象,它是 (空)。

我是IM pression下的 VIEWNAME 是由名称所暗示的查看 (如通过ASP.NET MVC控制器上此MS文章建议测试)。

BTW,当我测试的ViewData的,这一切都没有和正确的。

这里的code我有...

 公众的ActionResult指数(INT?页,字符串标记)
{
    如果(页面== NULL ||网页< = 0)
    {
        页面= 1;
    }    VAR可视数据=新IndexViewData
                       {
                       ......我的属性setter,等等。
                       };
    返回视图(可视数据);
}[测试方法]
公共无效Index_Action_Should_Return_Index_View_For_Default_HomePage()
{
    //安排。
    VAR控制器= PostController的; //包装,因为我用的去离子    //法案。
    的ViewResult的ViewResult = controller.Index(NULL,NULL)作为的ViewResult;    //断言。
    Assert.IsNotNull(的ViewResult);
    Assert.AreEqual(指数,viewResult.ViewName); //这是错误/失败。    VAR indexViewData = viewResult.ViewData.Model为IndexViewData;
    Assert.IsNotNull(indexViewData); // 这是真的。
}


解决方案

该VIEWNAME只有present当你在的ViewResult设置。如果你的视图名称匹配控制器的名字,然后我会检查,以确保VIEWNAME为空或空的,因为这将是(IMO)正确的行为,因为你不会想设置视图的名称。我只检查时我打算要被返回的视图不匹配的操作的视图名设置 - 返回错误的看法时,例如说,

修改:下面是ViewResultBase.cs为​​的ExecuteReuslt源(从RC1,我没有在我的Macintosh电脑,RTW的来源)。你可以看到它检查以查看是否VIEWNAME已直接设定,如果没有,它拉它从在控制器上下文的路径数据的动作。这只是发生在的ExecuteReuslt之后,您的控制器的动作完成时调用。

 公共覆盖无效的ExecuteReuslt(ControllerContext上下文){
        如果(上下文== NULL){
            抛出新的ArgumentNullException(上下文);
        }
        如果(String.IsNullOrEmpty(VIEWNAME)){
            VIEWNAME = context.RouteData.GetRequiredString(行动);
        }        ViewEngineResult结果= NULL;        如果(查看== NULL){
            结果= FindView(上下文);
            查看= result.View;
        }        ViewContext viewContext =新ViewContext(背景下,查看,ViewData的,TempData的);
        View.Render(viewContext,context.HttpContext.Response.Output);        如果(结果!= NULL){
            result.ViewEngine.ReleaseView(背景下,查看);
        }
    }

I'm doing some simple MS unit tests on my standard, nothing special controller.

When I check the ViewName proprty, from the returned ViewResult object, it's "" (empty).

I'm under the impression that the ViewName is implied by the name of the View (as suggested by this MS article on ASP.NET MVC controller testing).

BTW, when I test the ViewData, it's all there and correct.

Here's the code I have...

public ActionResult Index(int? page, string tag)
{
    if (page == null || page <= 0)
    {
        page = 1;
    }

    var viewData = new IndexViewData
                       {
                       ... my property setters, etc ...
                       };
    return View(viewData);
}

[TestMethod]
public void Index_Action_Should_Return_Index_View_For_Default_HomePage()
{
    // Arrange.
    var controller = PostController; // Wrapper, cause I use D.I.

    // Act.
    ViewResult viewResult = controller.Index(null, null) as ViewResult;

    // Assert.
    Assert.IsNotNull(viewResult);
    Assert.AreEqual("Index", viewResult.ViewName); // This is false/fails.

    var indexViewData = viewResult.ViewData.Model as IndexViewData;
    Assert.IsNotNull(indexViewData); // This is true.
}

解决方案

The ViewName is only present when you set it in the ViewResult. If your View name matches your controller name, then I would check to ensure that the ViewName is null or empty as that would be (IMO) the correct behavior since you wouldn't want to set a name on the view. I only check that the ViewName is set when I intend that the View to be returned does not match the action -- say, when returning the "Error" view, for example.

EDIT: The following is the source for ExecuteResult in ViewResultBase.cs (from RC1, I don't have the source for RTW on my Macintosh). As you can see it checks to see if the ViewName has been set directly and if not, it pulls it from the action in the controller context's route data. This only happens in ExecuteResult, which is invoked AFTER your controller's action has completed.

    public override void ExecuteResult(ControllerContext context) {
        if (context == null) {
            throw new ArgumentNullException("context");
        }
        if (String.IsNullOrEmpty(ViewName)) {
            ViewName = context.RouteData.GetRequiredString("action");
        }

        ViewEngineResult result = null;

        if (View == null) {
            result = FindView(context);
            View = result.View;
        }

        ViewContext viewContext = new ViewContext(context, View, ViewData, TempData);
        View.Render(viewContext, context.HttpContext.Response.Output);

        if (result != null) {
            result.ViewEngine.ReleaseView(context, View);
        }
    }

这篇关于单元测试我控制器的方法会导致一个空VIEWNAME?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆