Unit Testing Custom Password Validators in ASP.NET Core

跟風遠走 提交于 2019-12-01 07:39:19

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!