单元测试自定义MVC操作属性?

时间:2013-08-29 16:26:33

标签: c# asp.net-mvc unit-testing attributes

我从ActionFilterAttribute派生自定义属性,以处理在用户超时后异步加载的部分视图。这是我对OverActionExecuting的重写:

public override void OnActionExecuting(ActionExecutingContext filterContext)
{
  if (IsTimeoutAndAjax(filterContext))
  {
    /* First we ensure that the timeout view exists, and if not, we
     * throw an exception. Handling of the exception is upon the 
     * consumer.*/
    ViewEngineResult testView = ViewEngines.Engines.FindView(
        filterContext,
        TIMEOUT_VIEW_NAME,
        null);
    if (testView.View == null)
    {
      throw new Exception(
      "PartialTimeoutAttribute failure: the partial timeout view "
      + TIMEOUT_VIEW_NAME
      + " does not exist!");
    }

    /* Since this is a partial view result (or should be!) we're going
     * to return a partial view of our choosing rather than the expected
     * result.  The view contains an alert to let the user know their
     * session has expired then redirects the entire page to the login
     * form. A sample view of this type would look like this:
        @{
          Layout = null;
        }
        <script type="text/javascript">
            alert('Your session has timed out.  You will now be redirected to the login page.');
            window.location = '/Account/LogOn';
        </script>
     */

    PartialViewResult result = new PartialViewResult();
    result.ViewName = TIMEOUT_VIEW_NAME;
    filterContext.Result = result;
  }

  base.OnActionExecuting(filterContext);
}

有没有人为这种属性编写单元测试?我最好/唯一的赌注是模拟ActionExecutingContext吗?可选地,如果有一种更优雅的方式来处理整个场景(用户坐在页面上太长而且超时,点击一个按钮,将部分视图加载到目标div)我很乐意听到它。 / p>

0 个答案:

没有答案
相关问题