如何在MVC5中从AccountController模拟ApplicationUserManager

时间:2015-02-09 08:59:03

标签: asp.net-mvc unit-testing asp.net-mvc-5 moq asp.net-identity

我正在尝试在AccountController中为Register Method编写单元测试

我正在使用moq以及从Unit Test模拟ApplicationUserManager,ApplicationRoleManager和ApplicationSignInManager的正确方法。

public AccountController(ApplicationUserManager userManager, ApplicationRoleManager roleManager, ApplicationSignInManager signInManager)
{
    UserManager = userManager;
    RoleManager = roleManager;
    SignInManager = signInManager;

}

public ApplicationUserManager UserManager
    {
        get
        {
            return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set
        {
            _userManager = value;
        }
    }

private ApplicationSignInManager _signInManager;
    public ApplicationSignInManager SignInManager
    {
        get
        {
            return _signInManager ?? HttpContext.GetOwinContext().Get<ApplicationSignInManager>();
        }
        private set { _signInManager = value; }
    }

private ApplicationRoleManager _roleManager;
    public ApplicationRoleManager RoleManager
    {
        get
        {
            return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>();
        }
        set 
        {
            _roleManager = value;
        }
    }

1 个答案:

答案 0 :(得分:9)

这可能不是你所需要的,但看看,也许你会得到这个想法。

AccountController.cs

[HttpGet]
[Route("register")]
[AllowAnonymous]
public ActionResult Register()
{
    if (IsUserAuthenticated)
    {
        return RedirectToAction("Index", "Home");
    }
    return View();
}

public bool IsUserAuthenticated
{
    get 
    { 
        return
        System.Web.HttpContext.Current.User.Identity.IsAuthenticated; 
    }
}

AccountController Tests.csp>

[Test]
public void GET__Register_UserLoggedIn_RedirectsToHomeIndex()
{
    // Arrange
    HttpContext.Current = CreateHttpContext(userLoggedIn: true);
    var userStore = new Mock<IUserStore<ApplicationUser>>();
    var userManager = new Mock<ApplicationUserManager>(userStore.Object);
    var authenticationManager = new Mock<IAuthenticationManager>();
    var signInManager = new Mock<ApplicationSignInManager>(userManager.Object, authenticationManager.Object);

    var accountController = new AccountController(
        userManager.Object, signInManager.Object, authenticationManager.Object);

    // Act
    var result = accountController.Register();

    // Assert
    Assert.That(result, Is.TypeOf<RedirectToRouteResult>());
}

[Test]
public void GET__Register_UserLoggedOut_ReturnsView()
{
    // Arrange
    HttpContext.Current = CreateHttpContext(userLoggedIn: false);
    var userStore = new Mock<IUserStore<ApplicationUser>>();
    var userManager = new Mock<ApplicationUserManager>(userStore.Object);
    var authenticationManager = new Mock<IAuthenticationManager>();
    var signInManager = new Mock<ApplicationSignInManager>(userManager.Object, authenticationManager.Object);

    var accountController = new AccountController(
        userManager.Object, signInManager.Object, authenticationManager.Object);

    // Act
    var result = accountController.Register();

    // Assert
    Assert.That(result, Is.TypeOf<ViewResult>());
}

private static HttpContext CreateHttpContext(bool userLoggedIn)
{
    var httpContext = new HttpContext(
        new HttpRequest(string.Empty, "http://sample.com", string.Empty),
        new HttpResponse(new StringWriter())
    )
    {
        User = userLoggedIn
            ? new GenericPrincipal(new GenericIdentity("userName"), new string[0])
            : new GenericPrincipal(new GenericIdentity(string.Empty), new string[0])
    };

    return httpContext;
}