【发布时间】:2012-09-17 18:01:21
【问题描述】:
我正在 asp.net mvc 项目中测试帐户控制器。我测试了所有方法并查看了代码覆盖率结果,我注意到 Initialize 方法没有被覆盖。
如何测试这个方法?
public class AccountController : Controller
{
public IFormsAuthenticationService FormsService { get; set; }
public IMembershipService MembershipService { get; set; }
protected override void Initialize(RequestContext requestContext)
{
if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
if (MembershipService == null) { MembershipService = new AccountMembershipService(); }
base.Initialize(requestContext);
}
我试过这个测试方法,但出现错误。
[TestMethod]
public void Constructor_ReturnsController()
{
RequestContext requestContext = new RequestContext(new MockHttpContext(), new RouteData());
AccountController controller = new AccountController
{
FormsService = null,
MembershipService = null,
Url = new UrlHelper(requestContext),
};
IController cont = controller;
cont.Execute(requestContext);
}
错误信息:
测试方法 MVC3Project.Tests.Controllers.AccountControllerTests+AccountControllerTest.Constructor_ReturnsController 抛出异常: System.NotImplementedException: 方法或操作未实现。
【问题讨论】:
标签: asp.net-mvc unit-testing tdd nunit