gpt4 book ai didi

c# - 在测试中创建 UserManager 的实例

转载 作者:行者123 更新时间:2023-11-28 20:35:38 27 4
gpt4 key购买 nike

我对模拟的概念很陌生,我在注入(inject)/模拟 UserManager<T> 方面遇到了困难。我的测试

我需要使用 await _userManager.CreateAsync(user, password);

我找到了这个

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;
}

但这会抛出

System.NotSupportedException : Store does not implement IUserPasswordStore.

我怎样才能创建一个有效的 UserManager<T>测试中的实例?

我正在使用 Xunit

最佳答案

首先修改你的方法,我读过一些不同的论文,人们说你不需要测试已经测试过的库和工具。但有时我们需要模拟 UserManager 来注入(inject)我们的服务并解决这些依赖关系。UserManager 可以模拟得非常简单,但是像这样的错误消息并没有得到很好的解释。我们必须首先模拟 UserStore 并将其交给 UserManager,要模拟 UserStore,您可以创建一个派生 IUserStore 的类 MockUserStore code> 和 IUserPasswordStore :

public class MockUserStore : IUserStore<TUser>, IUserPasswordStore<TUser>

这个模拟类必须实现 IUserPasswordStore 的一些方法,否则你会收到错误消息!

public Task SetPasswordHashAsync(TUser user, string passwordHash,
CancellationToken cancellationToken)
{
user.PasswordHash = passwordHash;
return Task.FromResult(0);
}

public Task<string> GetPasswordHashAsync(TUser user, CancellationToken cancellationToken)
{
return Task.FromResult<string>(user.PasswordHash);
}

public Task<bool> HasPasswordAsync(TUser user, CancellationToken cancellationToken)
{
return Task.FromResult<bool>(!String.IsNullOrEmpty(user.PasswordHash));
}

您还可以使用一个类来模拟 UserManager 并用模拟方法覆盖它的方法:

public class MockUserManager : UserManager<TUser>
{
public MockUserManager(IUserStore<TUser> store, IOptions<IdentityOptions> optionsAccessor,
IPasswordHasher<TUser> passwordHasher, IEnumerable<IUserValidator<TUser>> userValidators,
IEnumerable<IPasswordValidator<TUser>> passwordValidators, ILookupNormalizer keyNormalizer,
IdentityErrorDescriber errors, IServiceProvider services, ILogger<UserManager<TUser>> logger)
: base(store, optionsAccessor, passwordHasher, userValidators, passwordValidators, keyNormalizer, errors, services, logger)
{
}

public override Task<IdentityResult> CreateAsync(TUser user)
{
return Task.FromResult(IdentityResult.Success);
}
}

毕竟你可以通过传递模拟的 UserStore 来模拟 UserManager:

    var userStore = new MockUserStore();
var userManager = new MockUserManager(userStore,
new Mock<IOptions<IdentityOptions>>().Object,
new Mock<IPasswordHasher<AppUser>>().Object,
new IUserValidator<TUser>[0],
new IPasswordValidator<TUser>[0],
new Mock<ILookupNormalizer>().Object,
new Mock<IdentityErrorDescriber>().Object,
new Mock<IServiceProvider>().Object,
new Mock<ILogger<UserManager<TUser>>>().Object);
return userManager;

关于c# - 在测试中创建 UserManager<T> 的实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54921473/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com