我正在 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:方法或操作未实现。