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

时间:2023-02-26
本文介绍了如何在 .Net Core 测试中模拟 UserManager?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

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

I have following code. Im trying to running a test case for create user.Following is what i have tried so far.

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

我的用户管理员在这里:

My User manager is here:

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

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

and my Command handler is this:

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

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

when i'm running my test it fails and saying Object reference not set to an instant of an object.

我在这里做错了什么??

What am i doing wrong here??

推荐答案

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

I know this is months old but I keep getting back to this thread. I will extend my own answer on this topic because just pointing to Haok's GitHub example is like saying: "Read a book" as it is huge. It does not pinpoint the issue and what you need to do. You need to isolate a Mock object, but not only that but also you need to 'Setup' the method for 'CreateAsync'. So let's put this in three parts:

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

假设我有一个返回模拟用户管理器的辅助方法.与 Haok 代码略有不同:

So say I have a helper method for returning a Mocked UserManager. Which is just slightly altered from the Haok code:

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",这也是我将要测试的内容以及注入此列表的内容.类似于我的示例:

What is key to this is I am injecting a generic 'TUser' that is what I will be testing as well injecting a list of this. Similar to my example of:

 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; 

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

Then finally I am testing a pattern with a repository similar to this implementation I want to test:

 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,而且还提供了一个回调,因此我实际上可以看到我注入的列表增加了.希望这对某人有所帮助.

The key to what I did is that not only did I 'Setup' the CreateAsync but I provided a callback so I can actually see my list I inject get incremented. Hope this helps someone.

这篇关于如何在 .Net Core 测试中模拟 UserManager?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

上一篇:如何模拟/伪造 ASP.Net Web 表单中的会话对象? 下一篇:Moq Params TargetParameterCountException:参数计数不匹配异常

相关文章

最新文章