提前感谢您的时间。我是学生,我们必须对我们的Asp.net核心2 Web应用程序进行单元测试。该应用程序具有身份验证,我们必须测试帐户控制器。我尝试了很多东西,但我甚至无法实例化控制器。
我的帐户控制器具有以下构造函数:
public AccountController(
UserManager<ApplicationUser> userManager,
SignInManager<ApplicationUser> signInManager,
IEmailSender emailSender,
ILogger<AccountController> logger,
RoleManager<IdentityRole> roleManager,
ApplicationDbContext context)
{
_userManager = userManager;
_signInManager = signInManager;
_emailSender = emailSender;
_logger = logger;
_rolemanager = roleManager;
_context = context;
}
正在测试的方法:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Login(LoginViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
// This doesn't count login failures towards account lockout
// To enable password failures to trigger account lockout, set lockoutOnFailure: true
var result =
await _signInManager.PasswordSignInAsync(model.NumeroInterno, model.Password, model.RememberMe,
false);
if (result.Succeeded)
{
_logger.LogInformation("User logged in.");
//return RedirectToLocal(returnUrl);
return RedirectToAction("Index", "Home");
}
if (result.IsLockedOut)
{
_logger.LogWarning("User account locked out.");
return RedirectToAction(nameof(Lockout));
}
ModelState.AddModelError(string.Empty, "Tentativa de Login inválida (dados incorretos).");
return View(model);
}
// If we got this far, something failed, redisplay form
return View(model);
}
由于