我正在使用
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();
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>();