为 Asp.net mvc 3 控制器编写测试时 MSpec 和基类测试失败

发布于 2024-11-01 08:55:57 字数 2232 浏览 5 评论 0原文

我目前正在为这个已经实现的控制器编写一些 MSpec 规范(是的,我知道这样做是“错误”的)。

这是一个“简单”的问题,但我不确定我做错了什么,希望有人能够指出我的方法的错误。

我的控制器的方法如下:

 public ActionResult Add()
    {
        this.SetPageTitle("Add something");
        return this.View();
    }

我遇到的问题是

this.SetPageTitle("Add something");

该方法是在定义当前控制器(CompanyHomeController)的基类(BaseController)中定义的。

我的规范如下:

[Subject(typeof(CompanyHomeController))]
public class when_the_company_add_page_is_requested
{
    static string pageTitle;

    static ActionResult result;

    static CompanyHomeController companyHomeController;

    // Arrange
    Establish a_company_home_controller_context = () =>
        {
            var companyDao = A.Fake<ICompanyDao>();
            companyHomeController = new CompanyHomeController(companyDao);

            pageTitle = "Add something";
        };

    // Act
    Because of = () => result = companyHomeController.Add();

    // Assert
    private It should_display_a_view = () => result.ShouldBeAView();

}

当我运行测试时,它失败了,我收到了这条消息:

System.NullReferenceException: Object reference not set to an instance of an object.
at ..Commons.Hosts.Web.Mvc.Base.BaseController.SetPageTitle(String title) in C:\Projects\Commons\.Commons.Hosts\Web\Mvc\Base\BaseController.cs:line 87
at ..Hosts.Web.Areas.Company.Controllers.CompanyHomeController.Add() in C:\Projects\\Hosts\.Hosts.Web\Areas\Company\Controllers\CompanyHomeController.cs:line 93
at ..Hosts.Web.Specs.Areas.Company.Controllers.when_the_company_add_page_is_requested.<.ctor>b__1() in C:\Projects\Hosts\.Hosts.Web.Specs\Areas\Company\Controllers\CompanyHomeControllerSpecs.cs:line 43
at Machine.Specifications.Utility.RandomExtensionMethods.InvokeAll(IEnumerable`1 actions) in d:\BuildAgent-02\work\9f23de4d4da9eb12\Source\Machine.Specifications\Utility\RandomExtensionMethods.cs:line 32
at Machine.Specifications.Model.Context.EstablishContext() in d:\BuildAgent-02\work\9f23de4d4da9eb12\Source\Machine.Specifications\Model\Context.cs:line 86

我觉得它是显而易见的东西,但我不太能看到它。由于 CompanyHomeController 是 SUT,我不太明白为什么我必须触及 BaseController。我应该把后者剔除吗?如果是这样,为什么?

I am currently writing some MSpec specs for this controller which has already been implemented (yes I know doing it the "wrong" way round).

It is a "simple" problem but I'm not sure what I'm doing wrong and hopefully someone will be able to point the error of my ways.

My Controller's method is as follows:

 public ActionResult Add()
    {
        this.SetPageTitle("Add something");
        return this.View();
    }

The issue I'm having is with

this.SetPageTitle("Add something");

The method is defined in a base class (BaseController) from which the current controller (CompanyHomeController) is defined.

My spec is as follows:

[Subject(typeof(CompanyHomeController))]
public class when_the_company_add_page_is_requested
{
    static string pageTitle;

    static ActionResult result;

    static CompanyHomeController companyHomeController;

    // Arrange
    Establish a_company_home_controller_context = () =>
        {
            var companyDao = A.Fake<ICompanyDao>();
            companyHomeController = new CompanyHomeController(companyDao);

            pageTitle = "Add something";
        };

    // Act
    Because of = () => result = companyHomeController.Add();

    // Assert
    private It should_display_a_view = () => result.ShouldBeAView();

}

When I run the test, it fails and I get this message:

System.NullReferenceException: Object reference not set to an instance of an object.
at ..Commons.Hosts.Web.Mvc.Base.BaseController.SetPageTitle(String title) in C:\Projects\Commons\.Commons.Hosts\Web\Mvc\Base\BaseController.cs:line 87
at ..Hosts.Web.Areas.Company.Controllers.CompanyHomeController.Add() in C:\Projects\\Hosts\.Hosts.Web\Areas\Company\Controllers\CompanyHomeController.cs:line 93
at ..Hosts.Web.Specs.Areas.Company.Controllers.when_the_company_add_page_is_requested.<.ctor>b__1() in C:\Projects\Hosts\.Hosts.Web.Specs\Areas\Company\Controllers\CompanyHomeControllerSpecs.cs:line 43
at Machine.Specifications.Utility.RandomExtensionMethods.InvokeAll(IEnumerable`1 actions) in d:\BuildAgent-02\work\9f23de4d4da9eb12\Source\Machine.Specifications\Utility\RandomExtensionMethods.cs:line 32
at Machine.Specifications.Model.Context.EstablishContext() in d:\BuildAgent-02\work\9f23de4d4da9eb12\Source\Machine.Specifications\Model\Context.cs:line 86

I feel it's something blooming obvious, but I can't quite see it. As the CompanyHomeController is the SUT, I don't quite see why I'd have to touch upon the BaseController. Should I be stubbing the latter out? If so, why?

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

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

发布评论

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

评论(1

绮烟 2024-11-08 08:55:57

SetPageTitle 可能正在尝试访问 HttpContext,它在单元测试中不可用,您可能会需要模拟。不幸的是,由于您没有展示此方法,因此很难提供更多帮助。

The SetPageTitle is probably trying to access the HttpContext which is not available in the unit test and which you might need to mock. Unfortunately as you haven't shown this method it is difficult to provide more help.

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