以下是AccountController API中的 ChangePassword Web方法。
D
F
A
G
G
以下是我在.NET Core 2.0中的XUnit中的单元测试
[HttpPost]
[Route("changepassword")]
public async Task<IActionResult> ChangePassword([FromBody]ChangePasswordModel model)
{
var user = await userManager.FindByNameAsync(this.User.Identity.Name);
KanbanResult kanbanResult = new KanbanResult();
if (model.NewPassword == model.ConfirmPassword)
{
var result = await userManager.ChangePasswordAsync(user, model.CurrentPassword, model.NewPassword );
if (result.Succeeded)
{
return Ok(KanbanResult.CreateOkResult(string.Empty));
}
else
return BadRequest(KanbanResult.CreateErrorResult( result.Errors.Select( er => er.Description).ToList()));
}
else
{
return BadRequest(KanbanResult.CreateErrorResult(new List<string>{"Confirmed password is not matching"}));
}
}
每当测试执行 ChangePassowrd 方法时,它会查找Controller.User对象,该对象在运行测试时为null。这是Asp.NET Core 2.0 API和测试。我应该如何初始化User或其他依赖属性以成功运行测试。