我有以下代码,我不知道哪些正确的单元测试我已经写出了这些方法以及如何完成。基本上我想使用NUnit.Framework。
提前感谢您提供任何线索!
[AllowAnonymous]
public ActionResult ForgotPassword(string id)
{
var model = new ForgotPasswordViewModel();
if (!string.IsNullOrEmpty(id))
{
#region Process Reset Password Key
try
{
var forgotPasswordEvent = AppModel.ForgotPasswordEvents.SingleOrDefault(x => x.UIDHash == id);
if (forgotPasswordEvent != null)
{
var stringToHash = string.Format("{0}---{1}---{2}", forgotPasswordEvent.UID.ToString(),
forgotPasswordEvent.UserId.ToString(), forgotPasswordEvent.Created.ToString());
var readyHash = SecurityHelper.GetHashString(stringToHash);
if (id == readyHash)
{
var forgotPasswordEventUserId = forgotPasswordEvent.UserId.ToString();
var realUser = AppModel.AspNetUsers.SingleOrDefault(x => x.Id == forgotPasswordEventUserId);
if (realUser != null)
{
var resetPasswordViewModel = new ResetPasswordViewModel();
resetPasswordViewModel.ResetPasswordData = id;
resetPasswordViewModel.UserName = realUser.UserName;
return RedirectToAction("ResetPassword", "Account", resetPasswordViewModel); // ResetPassword(resetPasswordViewModel);
}
}
}
else
{
return RedirectToAction("Index", "Home");
}
}
catch (Exception)
{
}
#endregion
}
#region Check if the user is logged in and fill out fileds for him.
var sessionManager = SessionWrapper.GetFromSession<SessionManager>("_SessionManager");
if (sessionManager != null)
{
var clientId = sessionManager.AppUser.ClientId;
if (clientId != null)
{
model.Email = sessionManager.AppUser.EmailID;
model.UserName = sessionManager.AppUser.UserName;
model.IsLoggedInUser = true;
}
}
#endregion
return View(model);
}
[HttpPost]
[AllowAnonymous]
public ActionResult ForgotPassword(ForgotPasswordViewModel model, FormCollection formCollection)
{
if (ModelState.IsValid)
{
try
{
#region Check user input
var user = AppModel.AspNetUsers.SingleOrDefault(x => x.UserName == model.UserName);
var areErrors = false;
if (user == null)
{
ModelState.AddModelError("UserDoesnotExist", DLMModelEntities.Properties.Resource.UserDoesNotExist);
areErrors = true;
}
if (user.EmailID != model.Email)
{
ModelState.AddModelError("EmailIsWrong", DLMModelEntities.Properties.Resource.EmailIsWrong);
areErrors = true;
}
if (areErrors)
return View(model);
#endregion
#region Send Email and inform user
try
{
var forgotPasswordEvent = new ForgotPasswordEvent();
var resetPasswordEmailUserState = new ResetPasswordEmailUserState();
resetPasswordEmailUserState.ForgotPasswordEventId = Guid.NewGuid();
resetPasswordEmailUserState.UserId = Guid.Parse(user.Id);
resetPasswordEmailUserState.Created = DateTime.Now;
forgotPasswordEvent.UID = resetPasswordEmailUserState.ForgotPasswordEventId;
forgotPasswordEvent.UserId = resetPasswordEmailUserState.UserId;
forgotPasswordEvent.IsSent = false;
forgotPasswordEvent.Created = resetPasswordEmailUserState.Created;
var stringToHash = string.Format("{0}---{1}---{2}", resetPasswordEmailUserState.ForgotPasswordEventId.ToString(),
resetPasswordEmailUserState.UserId.ToString(), resetPasswordEmailUserState.Created.ToString());
forgotPasswordEvent.UIDHash = SecurityHelper.GetHashString(stringToHash);
AppModel.ForgotPasswordEvents.Add(forgotPasswordEvent);
AppModel.SaveChanges();
var smtp = (SmtpSection)ConfigurationManager.GetSection("system.net/mailSettings/smtp");
// Set the MailerModel properties that will be passed to the MvcMailer object.
var m = new MailerModel();
m.UserName = user.UserName;
m.ResetPasswordLink = string.Format("{0}/{1}", Request.Url.AbsoluteUri, forgotPasswordEvent.UIDHash);
m.FromEmail = smtp.From;
m.Subject = AppConfiguration.ResetEmailSubject;
m.ToEmail = model.Email;
var client = new SmtpClientWrapper();
client.SendCompleted += (sender, e) =>
{
if (e.Error != null || e.Cancelled)
{
// Handle Error
}
else
{
try
{
var forgotPasswordEventsToUpdate = AppModel.ForgotPasswordEvents.SingleOrDefault(x => x.UID == resetPasswordEmailUserState.ForgotPasswordEventId);
if (forgotPasswordEventsToUpdate != null)
{
forgotPasswordEventsToUpdate.IsSent = true;
AppModel.SaveChanges();
}
}
catch (Exception ex)
{
ModelState.AddModelError("EmailEx", ex.Message);
}
}
};
Mailer.PasswordReset(m).SendAsync(resetPasswordEmailUserState, client);
model.IsResetEMailSent = true;
}
catch (Exception ex)
{
ModelState.AddModelError("EmailEx", ex.Message);
}
#endregion
}
catch (Exception ex)
{
ModelState.AddModelError("EmailEx", ex.Message);
}
}
return View(model);
}
答案 0 :(得分:2)
因为你的代码看起来很乱,不知道一个责任。
对于初学者,你可以在这里做的是:
如果您对以上几点有任何疑问,请与我联系。