c# – 单元测试asp.net mvc restful routing FormatResult

我正在使用 restful routing module for asp.net mvc并且非常满意.但我不能得到一件事.例如,我有一个像这样的控制器动作:

public ActionResult Index()
{
    if (Request.IsAjaxRequest()) 
        return PartialView();
    return View();
}

编写这样的规范没问题:

[Subject(typeof(LotsController))]
public class When_Index_called
{
    static LotsController controller;

    static ActionResult actionResult;

    Establish context = () => {
        controller = mocker.Create<LotsController>();
        controller.ControllerContext = Contexts.Controller.Default;
    };

    Because of = () => actionResult = controller.Index();

    It should_render_view = () => actionResult.AssertVieWrendered().ForViewWithDefaultName();

但是使用休息时,我希望有一个像这样的Index方法

public ActionResult Index()
{
    return RespondTo(format => {
        format.Html = () => {
            if (Request.IsAjaxRequest()) 
                return PartialView();
            return View();
        };
        format.Json = () => Json(new { });
    });
}

确定以前的规范失败了,因为动作结果不是ViewResult类型,它的类型是FormatResult. FormatResult本身会覆盖返回void的ExecuteResult方法.如果我想在FormatResult中验证操作结果类型和数据,我该如何对这种情况进行单元测试?

解决方法

在将来版本的restful routing中,这样的代码是可能的:

var formatResult = actionResult as FormatResult;
ActionResult result = formatResult.ExposeResult().Html();
result.ShouldBeOfType<ViewResult>();

相关文章

目录简介使用JS互操作使用ClipLazor库创建项目使用方法简单测...
目录简介快速入门安装 NuGet 包实体类User数据库类DbFactory...
本文实现一个简单的配置类,原理比较简单,适用于一些小型项...
C#中Description特性主要用于枚举和属性,方法比较简单,记录...
[TOC] # 原理简介 本文参考[C#/WPF/WinForm/程序实现软件开机...
目录简介获取 HTML 文档解析 HTML 文档测试补充:使用 CSS 选...