public ActionResult ChangePassword(ChangePasswordModel model) { if (ModelState.IsValid) { if (MembershipService.ChangePassword(User.Identity.Name, model.OldPassword, model.NewPassword)) { return RedirectToAction("ChangePasswordSuccess"); } else { ModelState.AddModelError("", "当前密码不正确或新密码无效。"); } } // 如果我们进行到这一步时某个地方出错,则重新显示表单 ViewData["PasswordLength"] = MembershipService.MinPasswordLength; return View(model); }
public void ChangePassword_Post_ReturnsRedirectOnSuccess() { // 排列 AccountController controller = GetAccountController(); ChangePasswordModel model = new ChangePasswordModel() { OldPassword = "******", NewPassword = "******", ConfirmPassword = "******" }; // 操作 ActionResult result = controller.ChangePassword(model); // 断言 Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); RedirectToRouteResult redirectResult = (RedirectToRouteResult)result; Assert.AreEqual("ChangePasswordSuccess", redirectResult.RouteValues["action"]); }
public void ChangePassword_Post_ReturnsViewIfChangePasswordFails() { // 排列 AccountController controller = GetAccountController(); ChangePasswordModel model = new ChangePasswordModel() { OldPassword = "******", NewPassword = "******", ConfirmPassword = "******" }; // 操作 ActionResult result = controller.ChangePassword(model); // 断言 Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual("当前密码不正确或新密码无效。", controller.ModelState[""].Errors[0].ErrorMessage); Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]); }
public void ChangePassword_Post_ReturnsViewIfModelStateIsInvalid() { // 排列 AccountController controller = GetAccountController(); ChangePasswordModel model = new ChangePasswordModel() { OldPassword = "******", NewPassword = "******", ConfirmPassword = "******" }; controller.ModelState.AddModelError("", "虚拟错误消息。"); // 操作 ActionResult result = controller.ChangePassword(model); // 断言 Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]); }