【问题标题】:Set up test using MSpec with MSpecMVC, RedirectToAction and ViewData test condition problem使用 MSpec 设置测试与 MSpecMVC、RedirectToAction 和 ViewData 测试条件问题
【发布时间】:2010-12-03 16:05:56
【问题描述】:

在我的 HomeController 中,我有以下内容:

 public ActionResult Index()
 {
     SetModuleTitle("Welcome");

     return RedirectToAction( "DashBoard", "Home" );       
 }

并且 SetModuleTitle 在父类中定义如下:

public void SetModuleTitle(string title)
{
    ViewData["ModuleTitle"] = string.Format("PM4 - {0}", title);
}

这没什么好奇怪的。现在我正在尝试编写我的测试来测试 SetModuleTitle 方法:

 [Subject( typeof( HomeController ) )]
public class when_the_home_page_is_requested_by_logged_in_user_ : context_for_a_home_controller_for_logged_user
{
    static ActionResult result;

    Because of = () => result = HomeController.Index();

    It should_set_the_module_title = () => ( ( ViewResult ) result ).ViewData[ "ModuleTitle" ].ShouldEqual( "PM4 - Dashboard" );      
}

我正确地被告知

无法将“System.Web.Mvc.RedirectToRouteResult”类型的对象转换为“System.Web.Mvc.ViewResult”类型。

那么在这种情况下我将如何设置 MSpec 测试?

大卫

【问题讨论】:

    标签: asp.net-mvc-2 testing mspec


    【解决方案1】:

    好的,我想我已经明白我哪里出错了。但是,首先我必须提供由

    调用的代码

    return RedirectToAction("DashBoard", "Home");

    public ActionResult DashBoard()
        {
            SetModuleTitle("Dashboard");
    
            return View();
        }
    

    所以,如果我的理解是正确的,经过我的测试调用

    因为 = () => 结果 = HomeController.Index();

    RedirectToAction 对象被返回并且代码执行在那里停止,即它不调用在 RedirectToAction 中指定的控制器方法。这是有道理的,因为毕竟我们在这里做的是单元测试而不是集成测试。 所以,在这里测试 SetModuleTitle 方法是没有意义的。

    应该实现测试对方法 Dashboard 的调用的代码:

    [Subject(typeof(HomeController))]
    public class when_the_dashboard_page_is_requested_by_logged_in_user : context_for_a_home_controller_for_logged_user
    {
        static ActionResult result;
    
        Because of = () => result = HomeController.DashBoard();
    
        It should_set_the_module_title = () =>
            {
                ( ( ViewResult ) result ).ViewData[ "ModuleTitle" ].ShouldEqual( "PM4 - Dashboard" );
            };
    
        It should_return_the_dashboard_page = () => 
            result.ShouldBeAView().And().ShouldUseDefaultView();
    }
    

    如果知识渊博的人能够否认、证实或以其他方式了解我的理解,那就太好了。

    【讨论】:

    • 嘿 DavidS,看来我误解了你的目的。如果您想做的是测试 Dashboard 调用,那么是的,您现在走在正确的轨道上。
    • 谢谢塞尔吉。刚刚看到你的评论:)
    【解决方案2】:

    在您的控制器操作中,您执行return RedirectToAction,它返回一个RedirectToRouteResult 对象,而不是ViewResult,这就是它所抱怨的。

    为了将您的结果对象(在规范中)转换为ViewResult,您的操作的返回语句必须如下所示:

     public ActionResult Index()
     {
         //Some code here
    
         return View(/*here maybe a model object*/);       
     }
    

    为了修复您的测试,您只需要更改此行:

    It should_set_the_module_title = () => ((ViewResult)result ).ViewData[ "ModuleTitle" ].ShouldEqual("PM4 - Dashboard");
    

    对于这个:

    It should_set_the_module_title = () => ((RedirectToRouteResult)result).ViewData[ "ModuleTitle" ].ShouldEqual("PM4 - Dashboard");
    

    希望这会有所帮助。

    【讨论】:

    • 谢谢塞尔吉。明天我会尝试,但这会很有意义:)。我才刚刚开始使用 MVC 和 MSpec,还在学习如何、什么和为什么。
    • 嘿,Sergi,恐怕当我投射到“RedirectToRouteResult”时,没有可用的“ViewData”属性。还有什么建议吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多