title: Unit Testing Custom Password Validators in ASP.NET Core
body: <p>I have a CustomPasswordValidator.cs file that overrides PasswordValidator</p>
  
  <pre><code> public class CustomPasswordValidator : PasswordValidator&lt;AppUser&gt;
      {   //override the PasswordValidator functionality with the custom definitions
          public override async Task&lt;IdentityResult&gt; ValidateAsync(UserManager&lt;AppUser&gt; manager, AppUser user, string password)
          {
              IdentityResult result = await base.ValidateAsync(manager, user, password);
  
              List&lt;IdentityError&gt; errors = result.Succeeded ? new List&lt;IdentityError&gt;() : result.Errors.ToList();
  
              //check that the username is not in the password
              if (password.ToLower().Contains(user.UserName.ToLower()))
              {
                  errors.Add(new IdentityError
                  {
                      Code = "PasswordContainsUserName",
                      Description = "Password cannot contain username"
                  });
              }
  
              //check that the password doesn't contain '12345'
              if (password.Contains("12345"))
              {
                  errors.Add(new IdentityError
                  {
                      Code = "PasswordContainsSequence",
                      Description = "Password cannot contain numeric sequence"
                  });
              }
              //return Task.FromResult(errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray()));
              return errors.Count == 0 ? IdentityResult.Success : IdentityResult.Failed(errors.ToArray());
          }
      }
  </code></pre>
  
  <p>I am new to using Moq and xUnit. I am trying to create a unit test to ensure that the correct number of errors are produced (showing working code, with code that produced errors in comments):</p>
  
  <pre><code>//test the ability to validate new passwords with Infrastructure/CustomPasswordValidator.cs 
          [Fact]
          public async void Validate_Password()
          {
              //Arrange
              &lt;Mock&gt;&lt;UserManager&lt;AppUser&gt;&gt; userManager = new &lt;Mock&gt;&lt;UserManager&lt;AppUser&gt;&gt;(); //caused null exception, use GetMockUserManager() instead
              &lt;Mock&gt;&lt;CustomPasswordValidator&gt; customVal = new &lt;Mock&gt;&lt;CustomPasswordValidator&gt;(); //caused null result object use customVal = new &lt;CustomPasswordValidator&gt;() instead
              &lt;AppUser&gt; user = new &lt;AppUser&gt;
              user.Name = "user" 
              //set the test password to get flagged by the custom validator
              string testPwd = "Thi$user12345";
  
              //Act
              //try to validate the user password
              IdentityResult result = await customVal.ValidateAsync(userManager, user, testPwd);
  
              //Assert
              //demonstrate that there are two errors present
              List&lt;IdentityError&gt; errors = result.Succeeded ? new List&lt;IdentityError&gt;() : result.Errors.ToList();
              Assert.Equal(errors.Count, 2);
          }
  
  //create a mock UserManager class
          private Mock&lt;UserManager&lt;AppUser&gt;&gt; GetMockUserManager()
          {
              var userStoreMock = new Mock&lt;IUserStore&lt;AppUser&gt;&gt;();
              return new Mock&lt;UserManager&lt;AppUser&gt;&gt;(
                  userStoreMock.Object, null, null, null, null, null, null, null, null);
          }
  </code></pre>
  
  <p>The error occurs on the IdentityResult line, indicating that I cannot convert a Mock to UserManager and cannot convert Mock to AppUser class.</p>
  
  <p>EDIT: changed to include GetMockUserManager() needed to mock a UserManagerClass in ASP.NET core (<a href="https://stackoverflow.com/questions/21194324/mocking-new-microsoft-entity-framework-identity-usermanager-and-rolemanager">Mocking new Microsoft Entity Framework Identity UserManager and RoleManager</a>)</p>