首页 > 解决方案 > 为 ASP.NET MVC Identity 编写单元测试

问题描述

我的客户控制器如下所示:

public class CustomersController : Controller
{
    private ApplicationUserManager _userManager;

    public CustomersController()
    {
    }

    public CustomersController(ApplicationUserManager userManager)
    {
        UserManager = userManager;
    }

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

    ...
}

如何编写创建用户的单元测试?UserManagerUnitTesting 方面缺少类。

[TestClass]
public class StoreUnitTest
{
    [TestMethod]
    public void CreateUserTest()
    {
        User user = new User()
        {
            FirstName = "Bill",
            LastName = "Gates",
            UserName = "billg@microsoft.com",
            LoweredUserName = "billg@microsoft.com",
            Email = "billg@microsoft.com",
            LoweredEmail = "billg@microsoft.com"                    
        };

        // UserManager is missing!
        IdentityResult result = UserManager.Create(user, "password");

        Assert.IsTrue(result.Succeeded);
    }
}

标签: asp.netasp.net-mvcvisual-studiounit-testingasp.net-identity

解决方案


推荐阅读