Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc ASP.Net标识2删除密码模拟_Asp.net Mvc_Unit Testing_Moq_Asp.net Identity - Fatal编程技术网

Asp.net mvc ASP.Net标识2删除密码模拟

Asp.net mvc ASP.Net标识2删除密码模拟,asp.net-mvc,unit-testing,moq,asp.net-identity,Asp.net Mvc,Unit Testing,Moq,Asp.net Identity,我想让管理员可以更改用户的密码 我是这样做的: var result = await this.UserManager.RemovePasswordAsync(user.Id); if (result.Succeeded) { result = await this.UserManager.AddPasswordAsync(user.Id, viewModel.MotDePasse); if (result.Succeeded) { return Re

我想让管理员可以更改用户的密码

我是这样做的:

var result = await this.UserManager.RemovePasswordAsync(user.Id);

if (result.Succeeded)
{
    result = await this.UserManager.AddPasswordAsync(user.Id, viewModel.MotDePasse);

    if (result.Succeeded)
    {
        return RedirectToAction("Edit", new { Id = viewModel.UserName });
    }
    else
    {
        viewModel.Errors = string.Join("<br />", result.Errors);
    }
}
else
{
    viewModel.Errors = string.Join("<br />", result.Errors);
}
_mockUserStore = new Mock<IUserStore<ApplicationUser>>();
var passwordStore = this._mockUserStore
                        .As<IUserPasswordStore<ApplicationUser>>();
passwordStore.Setup(pm => pm.FindByIdAsync(It.IsAny<string>()))
             .Returns(Task.FromResult<ApplicationUser>(new ApplicationUser() { Id = "id", Nom = "Name1" }));

_userManager = new UserManager<ApplicationUser>(this._mockUserStore.Object);
var mockUserValidator = new Mock<IIdentityValidator<ApplicationUser>>();
mockUserValidator.Setup(uv => uv.ValidateAsync(It.IsAny<ApplicationUser>()))
                 .Returns(Task.FromResult<IdentityResult>(IdentityResult.Success));

this._userManager.UserValidator = mockUserValidator.Object;
var result=wait this.UserManager.RemovePasswordAsync(user.Id);
if(result.successed)
{
结果=等待this.UserManager.AddPasswordAsync(user.Id,viewModel.MotDePasse);
if(result.successed)
{
返回RedirectToAction(“编辑”,新的{Id=viewModel.UserName});
}
其他的
{
viewModel.Errors=string.Join(“
”,result.Errors); } } 其他的 { viewModel.Errors=string.Join(“
”,result.Errors); }
当我运行它时,它工作正常。 但是当尝试对其进行单元测试时,我(在结果对象中)得到一个错误:“Name不能为null”

在我的单元测试中,我有这样一个测试初始化:

var result = await this.UserManager.RemovePasswordAsync(user.Id);

if (result.Succeeded)
{
    result = await this.UserManager.AddPasswordAsync(user.Id, viewModel.MotDePasse);

    if (result.Succeeded)
    {
        return RedirectToAction("Edit", new { Id = viewModel.UserName });
    }
    else
    {
        viewModel.Errors = string.Join("<br />", result.Errors);
    }
}
else
{
    viewModel.Errors = string.Join("<br />", result.Errors);
}
_mockUserStore = new Mock<IUserStore<ApplicationUser>>();
var passwordStore = this._mockUserStore
                        .As<IUserPasswordStore<ApplicationUser>>();
passwordStore.Setup(pm => pm.FindByIdAsync(It.IsAny<string>()))
             .Returns(Task.FromResult<ApplicationUser>(new ApplicationUser() { Id = "id", Nom = "Name1" }));

_userManager = new UserManager<ApplicationUser>(this._mockUserStore.Object);
var mockUserValidator = new Mock<IIdentityValidator<ApplicationUser>>();
mockUserValidator.Setup(uv => uv.ValidateAsync(It.IsAny<ApplicationUser>()))
                 .Returns(Task.FromResult<IdentityResult>(IdentityResult.Success));

this._userManager.UserValidator = mockUserValidator.Object;
\u mockUserStore=new Mock();
var passwordStore=this.\u mockUserStore
.As();
passwordStore.Setup(pm=>pm.FindByIdAsync(It.IsAny()))
.Returns(Task.FromResult(新的ApplicationUser(){Id=“Id”,Nom=“Name1”}));
_userManager=newusermanager(this.\u mockUserStore.Object);

我怎样才能解决它?哪个对象的名称为空?

这是UserValidator可能因为没有设置用户名而拒绝用户更新。您可能只需在用户管理器上注销UserValidator

_userManager.UserValidator = null

我需要像这样模拟UserValidator:

var result = await this.UserManager.RemovePasswordAsync(user.Id);

if (result.Succeeded)
{
    result = await this.UserManager.AddPasswordAsync(user.Id, viewModel.MotDePasse);

    if (result.Succeeded)
    {
        return RedirectToAction("Edit", new { Id = viewModel.UserName });
    }
    else
    {
        viewModel.Errors = string.Join("<br />", result.Errors);
    }
}
else
{
    viewModel.Errors = string.Join("<br />", result.Errors);
}
_mockUserStore = new Mock<IUserStore<ApplicationUser>>();
var passwordStore = this._mockUserStore
                        .As<IUserPasswordStore<ApplicationUser>>();
passwordStore.Setup(pm => pm.FindByIdAsync(It.IsAny<string>()))
             .Returns(Task.FromResult<ApplicationUser>(new ApplicationUser() { Id = "id", Nom = "Name1" }));

_userManager = new UserManager<ApplicationUser>(this._mockUserStore.Object);
var mockUserValidator = new Mock<IIdentityValidator<ApplicationUser>>();
mockUserValidator.Setup(uv => uv.ValidateAsync(It.IsAny<ApplicationUser>()))
                 .Returns(Task.FromResult<IdentityResult>(IdentityResult.Success));

this._userManager.UserValidator = mockUserValidator.Object;
var mockUserValidator=new Mock();
mockUserValidator.Setup(uv=>uv.ValidateAsync(It.IsAny()))
.Returns(Task.FromResult(IdentityResult.Success));
这是._userManager.UserValidator=mockUserValidator.Object;

它可以工作。

您可以添加堆栈跟踪吗?UserManager可能正在调用测试中未设置的用户存储上的其他函数。或者完全不同的东西!;-)只要我没有捕获到任何异常,调用堆栈中就没有任何中断(或者我不知道如何获得更深入的调用堆栈):just result.Errors.Where you have.Returns(Task.FromResult(new ApplicationUser(){Id=“Id”,Nom=“Name1”});将其更改为include UserName=“someusername”,并且不要如上所示设置UserValidator。我没有包括这个答案,因为它可能不起作用!好。。。它不起作用:如果UserValidator!=无效的如果UserValidator==null,则ArgumentNullException。如果null不起作用,则始终可以实现一个IUserValidator,它是NoopValidatorThank。我只需要模拟UserValidator并设置ValidateAsync,如我的答案所示。