title: Unit Testing Password Validators in ASP.NET Core
body: <p>I have a CustomPasswordValidator.cs </p>
  
  <pre><code> public class CustomPasswordValidator
  {
      public int MinLength { get; set; }
      public int MaxLength { get; set; }
  
  
      public Task&lt;IdentityResult&gt; ValidateAsync(string item)
  
      {
          IdentityError errors = new IdentityError();
          MinLength = 8;
          MaxLength = 16;
          if (String.IsNullOrEmpty(item) || item.Length &lt; MinLength || item.Length &gt; MaxLength)
  
          {
  
              errors.Description = "password must be of minimum 8 characters ";
              return Task.FromResult(IdentityResult.Failed(errors));
          }
          //Minimum eight charachters and maximum 16 characters, at least one uppercase letter, one number and no spaces
          string pattern = @"^(?=.*\d+)(?=.*[a-zA-Z])[0-9a-zA-Z@#$%^&amp;*-_+={}|:‘,.?`~]{8,16}$";
          //@"^(?=.*[a-z])(?=.*\d)[A-Za-z\d$@$!%*?&amp;]{8,16}";
          if (!Regex.IsMatch(item, pattern))
  
          {
              errors.Description = "password must at least have one uppercase letter, one number and no spaces ";
              return Task.FromResult(IdentityResult.Failed(errors));
          }
  
          return Task.FromResult(IdentityResult.Success);
      }
  }
  </code></pre>
  
  <p>And a controller which calls the validator class :</p>
  
  <pre><code>   public class ValidatePasswordController : Controller
  {
      public IActionResult Index()
      {
          return View();
      }
  
      [HttpPost]
      public JsonResult ChangePassword(ChangePasswordRequest user)
      {
          //get user data 
          string Password = user.Password;
          CustomPasswordValidator CheckPWD = new CustomPasswordValidator();
          var result = CheckPWD.ValidateAsync(Password);
          var errors = result.Result.Errors.ToArray();
          string Description = errors[0].Description;
          return Json(Description);
      }
  }
  </code></pre>
  
  <p>I'm new to using  unit testing with TestTools.UnitTesting. I want to create a unit test class that ensures that the right result is being returned. How can I approach that</p>
  
  <pre><code>  class PasswordValidatorTest
  {
  
      [TestMethod]
      public void TestPWDValidation()
      {
          var password = "Thi$user12345";
          var w = new ValidatePasswordController() { }; //arrange
          //act
  
  
      }
  }
  </code></pre>