I am trying to test the ForgotPassword method of my Account Controller, and I need to know if it sends a mail or not. I don't mind using a sort of mock for the email service, but i don't know how to implement it. I'm a bit new in ASP.NET and Unit Testing.
Here's my test method
[TestMethod]
public void ForgotPassword_ExistentUser()
{
var sut = new AccountController(_applicationUserManager, _appSignInManager, _authenticationManagerMock, _context, _iregisterMock, _iCategoriesMock, _iProvincesMock);
CreateApplicationUserWithRole("User");
var forgotPasswordModel = new ForgotPasswordViewModel()
{
Email = "test@test.com"
};
var result = sut.ForgotPassword(forgotPasswordModel).Result;
Assert.IsInstanceOfType(result, typeof(ViewResult));
var viewResult = (ViewResult)result;
Assert.AreEqual("ForgotPasswordConfirmation", viewResult.ViewName);
}
And my ForgotPassword Post method
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> ForgotPassword(ForgotPasswordViewModel model)
{
if (ModelState.IsValid)
{
var user = await _userManager.FindByNameAsync(model.Email);
if (user == null || !(await _userManager.IsEmailConfirmedAsync(user.Id)))
{
TempData["message"] = "The email is not confirmed or the user has not been created yet";
return View(model);
}
// Send an email with this link
string code = await _userManager.GeneratePasswordResetTokenAsync(user.Id);
var callbackUrl = Url.Action("ResetPassword", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
var message = new IdentityMessage();
message.Body = "Please reset your password by clicking <a href=\"" + callbackUrl + "\">here</a>";
message.Subject = "Reset Password";
message.Destination = user.Email;
await _userManager.EmailService.SendAsync(message);
return RedirectToAction("ForgotPasswordConfirmation", "Account");
}
TempData["message"] = "There was a problem with the email";
return View(model);
}
Aucun commentaire:
Enregistrer un commentaire