gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-29 18:09:04 25 4
gpt4 key购买 nike

我有以下代码。我正在尝试为创建用户运行一个测试用例。以下是我到目前为止所尝试的。

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

当我运行我的测试时,它失败了,并显示对象引用未设置为对象的瞬间。

我在这里做错了什么??

最佳答案

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

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

假设我有一个返回 Mocked 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;
}

关键是我正在注入(inject)一个通用的“TUser”,我将测试它并注入(inject)一个列表。类似于我的示例:

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

我所做的事情的关键在于,我不仅“设置”了 CreateAsync,而且还提供了一个回调,这样我实际上可以看到我注入(inject)的列表增加了。希望这对某人有帮助。

关于c# - 如何在 .Net Core 测试中模拟 UserManager?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49165810/

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