public ActionResult Register(RegisterModel model) { if (ModelState.IsValid) { // ユーザーの登録を試みます MembershipCreateStatus createStatus = MembershipService.CreateUser(model.UserName, model.Password, model.Email); if (createStatus == MembershipCreateStatus.Success) { FormsService.SignIn(model.UserName, false /* createPersistentCookie */); return RedirectToAction("Index", "Home"); } else { ModelState.AddModelError("", AccountValidation.ErrorCodeToString(createStatus)); } } // ここで問題が発生した場合はフォームを再表示します ViewData["PasswordLength"] = MembershipService.MinPasswordLength; return View(model); }
public void Register_Post_ReturnsViewIfModelStateIsInvalid() { // 準備 AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Email = "goodEmail", Password = "******", ConfirmPassword = "******" }; controller.ModelState.AddModelError("", "ダミー エラー メッセージ。"); // 実行 ActionResult result = controller.Register(model); // アサート Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult viewResult = (ViewResult)result; Assert.AreEqual(model, viewResult.ViewData.Model); Assert.AreEqual(10, viewResult.ViewData["PasswordLength"]); }
public void Register_Post_ReturnsViewIfRegistrationFails() { // 準備 AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Email = "goodEmail", Password = "******", ConfirmPassword = "******" }; // 実行 ActionResult result = controller.Register(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 Register_Post_ReturnsRedirectOnSuccess() { // 準備 AccountController controller = GetAccountController(); RegisterModel model = new RegisterModel() { UserName = "******", Email = "goodEmail", Password = "******", ConfirmPassword = "******" }; // 実行 ActionResult result = controller.Register(model); // アサート Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); RedirectToRouteResult redirectResult = (RedirectToRouteResult)result; Assert.AreEqual("Home", redirectResult.RouteValues["controller"]); Assert.AreEqual("Index", redirectResult.RouteValues["action"]); }