为 Asp.net mvc 3 控制器编写测试时 MSpec 和基类测试失败
我目前正在为这个已经实现的控制器编写一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
SetPageTitle
可能正在尝试访问HttpContext
,它在单元测试中不可用,您可能会需要模拟。不幸的是,由于您没有展示此方法,因此很难提供更多帮助。The
SetPageTitle
is probably trying to access theHttpContext
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.