title: How to mock UserManager in .Net Core testing?
body: <p>I have following code. Im trying to running a test case for create user.Following is what i have tried so far. </p>
  
  <pre><code>public class CreateUserCommandHandlerTest
  {
      private Mock&lt;UserManager&lt;ApplicationUser&gt;&gt; _userManager;
      private CreateUserCommandHandler _systemUnderTest;
  
      public CreateUserCommandHandlerTest()
      {
          _userManager = MockUserManager.GetUserManager&lt;ApplicationUser&gt;();
          var user = new ApplicationUser() { UserName = "ancon1", Email = "ancon@mail.com", RoleType = RoleTypes.Anonymous };
          _userManager
              .Setup(u =&gt; 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&lt;Application.Commands.CreatedResponse&gt;(result);
      }
  }
  </code></pre>
  
  <p>My User manager is here:</p>
  
  <pre><code>public static class MockUserManager
  {
      public static Mock&lt;UserManager&lt;TUser&gt;&gt; GetUserManager&lt;TUser&gt;()
          where TUser : class
      {
          var store = new Mock&lt;IUserStore&lt;TUser&gt;&gt;();
          var passwordHasher = new Mock&lt;IPasswordHasher&lt;TUser&gt;&gt;();
          IList&lt;IUserValidator&lt;TUser&gt;&gt; userValidators = new List&lt;IUserValidator&lt;TUser&gt;&gt;
          {
              new UserValidator&lt;TUser&gt;()
          };
          IList&lt;IPasswordValidator&lt;TUser&gt;&gt; passwordValidators = new List&lt;IPasswordValidator&lt;TUser&gt;&gt;
          {
              new PasswordValidator&lt;TUser&gt;()
          };
          userValidators.Add(new UserValidator&lt;TUser&gt;());
          passwordValidators.Add(new PasswordValidator&lt;TUser&gt;());
          var userManager = new Mock&lt;UserManager&lt;TUser&gt;&gt;(store.Object, null, passwordHasher.Object, userValidators, passwordValidators, null, null, null, null);
          return userManager;
      }
  }
  </code></pre>
  
  <p>and my Command handler is this:</p>
  
  <pre><code> public class CreateUserCommandHandler : IRequestHandler&lt;CreateUserCommand, BaseCommandResponse&gt;
  {
      private readonly UserManager&lt;ApplicationUser&gt; _userManager;
  
      public CreateUserCommandHandler(UserManager&lt;ApplicationUser&gt; userManager)
      {
          _userManager = userManager;
      }
  
      public async Task&lt;BaseCommandResponse&gt; 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 =&gt; e.Description).First());
  
          return errorResponse;
      }
  }
  </code></pre>
  
  <p>when i'm running my test it fails and saying Object reference not set to an instant of an object.</p>
  
  <p>What am i doing wrong here??</p>