1

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

4

1 回答 1

1

您需要在单元测试中显式调用它:

[TestMethod]
public void AccountController_Initialize_Method_Should_WireUp_Dependencies()
{
    // arrange
    AccountController controller = new AccountController();

    // act
    controller.Initialize(null);

    // assert
    Assert.IsNotNull(controller.FormsService);
    Assert.IsNotNull(controller.MembershipService);
}

请注意,使用该Initialize方法设置依赖项是不好的做法。我建议您使用依赖注入:

public class AccountController : Controller
{

    public IFormsAuthenticationService FormsService { get; private set; }
    public IMembershipService MembershipService { get; private set; }

    public AccountController(IFormsAuthenticationService formsService, IMembershipService membershipService)
    {
        FormsService = formsService;
        MembershipService = membershipService;
    }

    ... actions
}

现在你可以设置你最喜欢的依赖注入框架来将这些依赖注入到你的控制器中。例如,使用 Ninject 非常简单。安装Ninject.MVC3NuGet,然后在生成的~/App_Start/NinjectWebCommon.cs文件中配置内核:

/// <summary>
/// Load your modules or register your services here!
/// </summary>
/// <param name="kernel">The kernel.</param>
private static void RegisterServices(IKernel kernel)
{
    kernel.Bind<IFormsAuthenticationService>().To<FormsAuthenticationService>();
    kernel.Bind<IMembershipService>().To<AccountMembershipService>();
}        

最后,您可以将这些依赖项模拟到您的单元测试中,然后定义对它们的期望。

于 2012-09-17T18:04:46.433 回答