为什么这个ViewResult会失败MSpecMVC的ShouldBeAView断言?

时间:2010-12-05 21:07:28

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

我一直关注James Broome's tutorial自学MSpec和一些相关的基础设施。我正在使用最新版本的MSpec,MSpecMvc和ASP.NET MVC 2,我没有使用J.P Boodhoo的库。

当我运行此测试时

[Subject(typeof(HomeController))]
public class when_the_home_controller_is_told_to_display_the_default_view
{
    static string key;
    static string message;
    static ActionResult result;
    static HomeController home_controller;

    Establish context = () =>
    {
        key = "Message";
        message = "Welcome to ASP.NET MVC!";
        home_controller = new HomeController();
    };

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

    It should_return_the_home_view = () => result.ShouldBeAView().And().ViewName.ShouldBeEmpty();
}

我收到以下错误

  

应返回主视图:失败
  应该是System.Web.Mvc.ViewResult类型,但类型为System.Web.Mvc.ViewResult

当我单步执行代码时,它会在此方法的断言中调用(在MSpecMVC的ActionResultExtensions.cs文件中)

public static ViewResultAnd ShouldBeAView(this ActionResult actionResult)
{
    actionResult.ShouldBeOfType<ViewResult>();
    return new ViewResultAnd(actionResult as ViewResult);
}

虽然,我可以确认actionResult的类型为System.Web.Mvc.ViewResult。我在另一台计算机上使用相同的工具来运行其他测试,但我没有遇到当前的问题。

1 个答案:

答案 0 :(得分:3)

James Broome的MSpec.MVC扩展使用Mspec v0.2。由于您使用的是Mspec v0.3,因此存在不匹配。您应该获取源并更新解决方案以使用MSpec v0.3。

确保Mspec.MVC扩展目标与ASP.NET MVC解决方案相同的.NET Framwork版本(例如,两者都是4.0)。这还取决于您使用的MSpec版本。 MSpec v0.3针对.NET 3.5.NET 4.0

进行编译