简体   繁体   English

测试 usermanager 创建用户但密码 hash 属性为 null

[英]Testing usermanager creates a user but password hash property is null

I am working with Microsoft.AspNetCore.Identity.UserManager and I'm trying to mock the creation of a new user.我正在使用 Microsoft.AspNetCore.Identity.UserManager 并试图模拟新用户的创建。 In fact, it does create a new user with username, email etc. but the password hash property is still null.事实上,它确实创建了一个用户名为 email 等的新用户,但密码 hash 属性仍然是 null。

This is how I set up mock usermanager with some additional setup:这就是我如何通过一些额外的设置来设置模拟用户管理器:

        var store = new Mock<IUserPasswordStore<User>>();

        var validator = new UserValidator<User>();
        var passValidator = new PasswordValidator<User>();

        var mgr = new Mock<UserManager<User>>(store.Object, null, null, null, null, null, null, null, null);
        mgr.Object.UserValidators.Add(validator);
        mgr.Object.PasswordValidators.Add(passValidator);
        mgr.Object.PasswordHasher = new PasswordHasher<User>();
        mgr.Object.Options = AuthenticationRules.DefaultAuthenticationRules();

        List<User> users= new List<User>();

        mgr.Setup(x => x.DeleteAsync(It.IsAny<User>())).ReturnsAsync(IdentityResult.Success);
        mgr.Setup(x => x.CreateAsync(It.IsAny<User>(), It.IsAny<string>())).ReturnsAsync(IdentityResult.Success).Callback<User, string>((x, y) => users.Add(x));
        mgr.Setup(x => x.UpdateAsync(It.IsAny<User>())).ReturnsAsync(IdentityResult.Success);

The 'DefaultAuthenticationRules' returns this: 'DefaultAuthenticationRules' 返回:

    public static IdentityOptions DefaultAuthenticationRules(IdentityOptions identityOptions = null)
    {
        if(identityOptions == null)
            identityOptions = new IdentityOptions();

        identityOptions.User.RequireUniqueEmail = true;
        identityOptions.Password.RequireNonAlphanumeric = false;
        identityOptions.Password.RequiredUniqueChars = 0;

        return identityOptions;
    }

I am then passing the mgr.Object to a method that handles the creation of the new user where 'Object' is _userManager然后,我将 mgr.Object 传递给处理创建新用户的方法,其中“对象”为 _userManager

        var creationResult = await _userManager.CreateAsync(_user, _registrationModel.Password);

        if (!creationResult.Succeeded)
            return false;

        return true;

_registrationModel.Password IS populated. _registrationModel.Password 已填充。

So now when the setup adding a new user in the callback to the list of users the user is populated without password hash.因此,现在当设置在回调中向用户列表添加新用户时,该用户在没有密码 hash 的情况下填充。 I am not exactly sure what I'm missing here.我不确定我在这里缺少什么。 I'm missing something in mgr.Setup?我在 mgr.Setup 中遗漏了一些东西?

Thanks in advance提前致谢

The callback function of yours Callback<User, string>((x, y) => users.Add(x));你的回调 function Callback<User, string>((x, y) => users.Add(x)); will be executed after the completion of the test, it will accept as a parameter the same parameter value that you pass to the mocked method, in your case the password is probably an empty string or null.将在测试完成后执行,它将接受您传递给模拟方法的相同参数值作为参数,在您的情况下,密码可能是空字符串或 null。

_registrationModel.Password IS populated. _registrationModel.Password 已填充。

Ok, but the inner code that generate some password does not have any influence on the (x,y) => {..} params.好的,但是生成一些密码的内部代码对(x,y) => {..}参数没有任何影响。

See this code (copied from this great answer):请参阅此代码(从this great answer复制):

public interface IFoo
{
    int Bar(bool b);
}

var mock = new Mock<IFoo>();

mock.Setup(mc => mc.Bar(It.IsAny<bool>()))
    .Callback<bool>(b => Console.WriteLine("Bar called with: " + b))
    .Returns(99999);

var ret1 = mock.Object.Bar(true);
Console.WriteLine("Result ret1: " + ret1);
var ret2 = mock.Object.Bar(false);
Console.WriteLine("Result ret2: " + ret2);

//OUTPUT:
//Result ret1: true.
//Result ret1: false.

In the example, regardless of what is happening inside the Bar method, the output will be depend only upon the calling param value of the mocked function.在示例中,无论Bar方法内部发生什么,output 将仅取决于模拟 function 的调用参数值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM