Unit Testing Custom Password Validators in ASP.NET Core

前端 未结 1 1035
轮回少年
轮回少年 2021-01-14 01:16

I have a CustomPasswordValidator.cs file that overrides PasswordValidator

 public class CustomPasswordValidator : PasswordValidator
    {   //         


        
相关标签:
1条回答
  • 2021-01-14 02:01

    With Moq you need call .Object on the mock to get the mocked object. You should also make the test async and await the method under test.

    You are also mocking the subject under test which in this case is cause the method under test to return null when called because it would not have been setup appropriately. You are basically testing the mocking framework at that point.

    Create an actual instance of the subject under test CustomPasswordValidator and exercise the test, mocking the explicit dependencies of the subject under test to get the desired behavior.

    public async Task Validate_Password() {
    
        //Arrange
        var userManagerMock = new GetMockUserManager();
        var subjetUnderTest = new CustomPasswordValidator();
        var user = new AppUser() {
            Name = "user" 
        }; 
        //set the test password to get flagged by the custom validator
        var password = "Thi$user12345";
    
        //Act
        IdentityResult result = await subjetUnderTest.ValidateAsync(userManagerMock.Object, user, password);
    
    
        //...code removed for brevity
    
    }
    

    Read Moq Quickstart to get more familiar with how to use moq.

    0 讨论(0)
提交回复
热议问题