我一直在关注 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();
}
我收到以下错误
should return the home view : Failed
Should be of type System.Web.Mvc.ViewResult but is of type 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
.我在另一台计算机上使用了相同的工具来运行其他测试,但我没有遇到当前的问题。
最佳答案
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
关于asp.net-mvc - 为什么这个 ViewResult 失败了 MSpecMVC 的 ShouldBeAView 断言?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4361185/