为什么此 ViewResult 失败 MSpecMVC 的 ShouldBeAView 断言?

发布于 2024-10-06 03:55:58 字数 1542 浏览 2 评论 0原文

我一直在关注 James Broome 的教程,用于自学 MSpec 和一些相关的基础设施。我正在使用最新版本的 MSpec、MSpecMvc 和 ASP.NET MVC 2 和我没有使用 JP 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 类型

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

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

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

I've been following James Broome's tutorial to teach myself MSpec and some of the related infrastructure. I'm using the latest version of MSpec, MSpecMvc, and ASP.NET MVC 2 and I'm not using J.P Boodhoo's libraries.

When I run this test

[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();
}

I get the following error

should return the home view : Failed
Should be of type System.Web.Mvc.ViewResult but is of type System.Web.Mvc.ViewResult

When I step through the code it conks in the assertion in this method (in the ActionResultExtensions.cs file of MSpecMVC)

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

Although, I can confirm that actionResult is of type System.Web.Mvc.ViewResult. I've used the same tools on another computer to run other tests but I've not encountered the current issue.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人事已非 2024-10-13 03:55:58

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

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

James Broome's MSpec.MVC extensions use Mspec v0.2. Since you are using Mspec v0.3 there is a mismatch. You should get the source and update the solution to use MSpec v0.3.

Make sure the Mspec.MVC extensions target the same .NET Framwork version as your ASP.NET MVC solution (for example, both are 4.0). That will also depend on which version of MSpec you are using. MSpec v0.3 is compiled against both .NET 3.5 and .NET 4.0

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文