如何在 .Net Core 测试中模拟 UserManager?

How to mock UserManager in .Net Core testing?

我有以下代码。我正在尝试 运行 一个用于创建 user.Following 的测试用例,这是我迄今为止所尝试过的。

public class CreateUserCommandHandlerTest
{
    private Mock<UserManager<ApplicationUser>> _userManager;
    private CreateUserCommandHandler _systemUnderTest;

    public CreateUserCommandHandlerTest()
    {
        _userManager = MockUserManager.GetUserManager<ApplicationUser>();
        var user = new ApplicationUser() { UserName = "ancon1", Email = "ancon@mail.com", RoleType = RoleTypes.Anonymous };
        _userManager
            .Setup(u => u.CreateAsync(user, "ancon2")).ReturnsAsync(IdentityResult.Success);
        _systemUnderTest = new CreateUserCommandHandler(_userManager.Object);
    }

    [Fact]
    public async void Handle_GivenValidInput_ReturnsCreatedResponse()
    {
        var command = new CreateUserCommand { Username = "ancon1", Email = "ancon@mail.com", Password = "ancon2", RoleType = RoleTypes.Anonymous };
        var result = await _systemUnderTest.Handle(command, default(CancellationToken));
        Assert.NotNull(result);
        Assert.IsType<Application.Commands.CreatedResponse>(result);
    }
}

我的用户经理在这里:

public static class MockUserManager
{
    public static Mock<UserManager<TUser>> GetUserManager<TUser>()
        where TUser : class
    {
        var store = new Mock<IUserStore<TUser>>();
        var passwordHasher = new Mock<IPasswordHasher<TUser>>();
        IList<IUserValidator<TUser>> userValidators = new List<IUserValidator<TUser>>
        {
            new UserValidator<TUser>()
        };
        IList<IPasswordValidator<TUser>> passwordValidators = new List<IPasswordValidator<TUser>>
        {
            new PasswordValidator<TUser>()
        };
        userValidators.Add(new UserValidator<TUser>());
        passwordValidators.Add(new PasswordValidator<TUser>());
        var userManager = new Mock<UserManager<TUser>>(store.Object, null, passwordHasher.Object, userValidators, passwordValidators, null, null, null, null);
        return userManager;
    }
}

我的命令处理程序是这样的:

 public class CreateUserCommandHandler : IRequestHandler<CreateUserCommand, BaseCommandResponse>
{
    private readonly UserManager<ApplicationUser> _userManager;

    public CreateUserCommandHandler(UserManager<ApplicationUser> userManager)
    {
        _userManager = userManager;
    }

    public async Task<BaseCommandResponse> Handle(CreateUserCommand createUserCommand, CancellationToken cancellationToken)
    {
        var user = new ApplicationUser { UserName = createUserCommand.Username, Email = createUserCommand.Email, RoleType = createUserCommand.RoleType };
        var result = await _userManager.CreateAsync(user, createUserCommand.Password);
        if (result.Succeeded)
        {
            return new CreatedResponse();
        }

        ErrorResponse errorResponse = new ErrorResponse(result.Errors.Select(e => e.Description).First());

        return errorResponse;
    }
}

当我 运行 我的测试失败并说对象引用未设置为对象的瞬间。

我哪里做错了??

aspnet/Identity 是开源的,所以您可以看看他们是如何模拟它的。

这是他们的做法:MockHelpers.cs

测试用户管理器

public static UserManager<TUser> TestUserManager<TUser>(IUserStore<TUser> store = null) where TUser : class
{
    store = store ?? new Mock<IUserStore<TUser>>().Object;
    var options = new Mock<IOptions<IdentityOptions>>();
    var idOptions = new IdentityOptions();
    idOptions.Lockout.AllowedForNewUsers = false;
    options.Setup(o => o.Value).Returns(idOptions);
    var userValidators = new List<IUserValidator<TUser>>();
    var validator = new Mock<IUserValidator<TUser>>();
    userValidators.Add(validator.Object);
    var pwdValidators = new List<PasswordValidator<TUser>>();
    pwdValidators.Add(new PasswordValidator<TUser>());
    var userManager = new UserManager<TUser>(store, options.Object, new PasswordHasher<TUser>(),
        userValidators, pwdValidators, new UpperInvariantLookupNormalizer(),
        new IdentityErrorDescriber(), null,
        new Mock<ILogger<UserManager<TUser>>>().Object);
    validator.Setup(v => v.ValidateAsync(userManager, It.IsAny<TUser>()))
        .Returns(Task.FromResult(IdentityResult.Success)).Verifiable();
    return userManager;
}

我知道这已经几个月了,但我一直回到这个话题。我将在这个主题上扩展我自己的答案,因为仅仅指向 Haok 的 GitHub 示例就像在说:“读一本书”,因为它很大。它没有指出问题以及您需要做什么。您需要隔离 Mock 对象,但不仅如此,您还需要 'Setup' 'CreateAsync' 的方法。所以让我们把它分成三个部分:

  1. 如果您使用 MOQ 或类似框架来模拟创建 UserManager,则需要 MOCK。
  2. 您需要设置您希望从中返回结果的 UserManager 方法。
  3. 您可能希望从模拟的 Entity Framework Core 2.1 或类似版本中注入一些通用列表,以便您可以实际看到 IDentity 用户列表实际增加或减少。不仅仅是 UserManager 成功了,没有别的

假设我有一个返回模拟 UserManager 的辅助方法。与 Haok 代码略有不同:

public static Mock<UserManager<TUser>> MockUserManager<TUser>(List<TUser> ls) where TUser : class
{
    var store = new Mock<IUserStore<TUser>>();
    var mgr = new Mock<UserManager<TUser>>(store.Object, null, null, null, null, null, null, null, null);
    mgr.Object.UserValidators.Add(new UserValidator<TUser>());
    mgr.Object.PasswordValidators.Add(new PasswordValidator<TUser>());

    mgr.Setup(x => x.DeleteAsync(It.IsAny<TUser>())).ReturnsAsync(IdentityResult.Success);
    mgr.Setup(x => x.CreateAsync(It.IsAny<TUser>(), It.IsAny<string>())).ReturnsAsync(IdentityResult.Success).Callback<TUser, string>((x, y) => ls.Add(x));
    mgr.Setup(x => x.UpdateAsync(It.IsAny<TUser>())).ReturnsAsync(IdentityResult.Success);

    return mgr;
}

这个的关键是我正在注入一个通用的 'TUser' 这就是我将要测试的以及注入一个列表。类似于我的例子:

 private List<ApplicationUser> _users = new List<ApplicationUser>
 {
      new ApplicationUser("User1", "user1@bv.com") { Id = 1 },
      new ApplicationUser("User2", "user2@bv.com") { Id = 2 }
 };
    
 ...

 private _userManager = MockUserManager<ApplicationUser>(_users).Object; 

最后,我正在使用类似于我要测试的实现的存储库来测试模式:

 public async Task<int> CreateUser(ApplicationUser user, string password) => (await _userManager.CreateAsync(user, password)).Succeeded ? user.Id : -1;

我是这样测试的:

 [Fact]
 public async Task CreateAUser()
 {
      var newUser = new ApplicationUser("NewUser", "New@test.com");
      var password = "P@ssw0rd!";

      var result = await CreateUser(newUser, password);

      Assert.Equal(3, _users.Count);
  }

我所做的事情的关键是,我不仅 'Setup' CreateAsync,而且还提供了回调,因此我实际上可以看到我注入的列表增加了。希望这对某人有所帮助。

在 .NetCore 2.2 中,您的做法略有不同。将其视为对@Nick Chapsas 回答的更新。

首先,您必须使用 IUserPasswordStore 而不是 IUserStore。 IUserPasswordStore 继承了IUserStore,但是UserManager 想得到IUserPasswordStore。换句话说,有些东西是行不通的。

如果您想测试 UserManager 的真实行为(例如 CreateUserAsync),您可以使用 UserValidator 和 PasswordValidator 的真实实现。您可能只想确保您的方法对 CreateUser 错误做出应有的反应。

这是我更新的示例:

UserManager<TUser> CreateUserManager() where TUser : class
{
    Mock<IUserPasswordStore<TUser>> userPasswordStore = new Mock<IUserPasswordStore<TUser>>();
    userPasswordStore.Setup(s => s.CreateAsync(It.IsAny<TUser>(), It.IsAny<CancellationToken>()))
        .Returns(Task.FromResult(IdentityResult.Success));

    var options = new Mock<IOptions<IdentityOptions>>();
    var idOptions = new IdentityOptions();

    //this should be keep in sync with settings in ConfigureIdentity in WebApi -> Startup.cs
    idOptions.Lockout.AllowedForNewUsers = false;
    idOptions.Password.RequireDigit = true;
    idOptions.Password.RequireLowercase = true;
    idOptions.Password.RequireNonAlphanumeric = true;
    idOptions.Password.RequireUppercase = true;
    idOptions.Password.RequiredLength = 8;
    idOptions.Password.RequiredUniqueChars = 1;

    idOptions.SignIn.RequireConfirmedEmail = false;

    // Lockout settings.
    idOptions.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(5);
    idOptions.Lockout.MaxFailedAccessAttempts = 5;
    idOptions.Lockout.AllowedForNewUsers = true;


    options.Setup(o => o.Value).Returns(idOptions);
    var userValidators = new List<IUserValidator<TUser>>();
    UserValidator<TUser> validator = new UserValidator<TUser>();
    userValidators.Add(validator);

    var passValidator = new PasswordValidator<TUser>();
    var pwdValidators = new List<IPasswordValidator<TUser>>();
    pwdValidators.Add(passValidator);
    var userManager = new UserManager<TUser>(userPasswordStore.Object, options.Object, new PasswordHasher<TUser>(),
        userValidators, pwdValidators, new UpperInvariantLookupNormalizer(),
        new IdentityErrorDescriber(), null,
        new Mock<ILogger<UserManager<TUser>>>().Object);

    return userManager;
}

请注意,如果您想从 UserManager 测试 CreateAsync,UserPasswordStore 有一个方法 (CreateAsync) 应该被模拟。

密码和锁定设置取自我的项目。它们应该与您的设置保持同步,以便您可以测试真实的东西。

当然你不会测试例如PasswordValidator,但是你可以测试你的方法,例如:

//Part of user service
public async Task<IdentityResult> Register(UserDto data)
{
    SystemUser user = ConvertDtoToUser(data);
    IdentityResult result = userManager.CreateAsync(user, data.Password);

    //some more code that is dependent on the result
}