public void CreateUser_creates_a_new_user() { // arrange var user = new Mock<MembershipUser>().Object; const string username = "******"; const string password = "******"; const string email = "*****@*****.**"; const string passwordQuestion = "What is your quest?"; const string passwordAnswer = "To seek the Holy Grail"; _userService.Setup(x => x.Create(username, password, email, passwordQuestion, passwordAnswer)).Returns(user).Verifiable(); // act var viewModel = new CreateUserViewModel { Username = username, Password = password, Email = email, PasswordQuestion = passwordQuestion, PasswordAnswer = passwordAnswer }; _controller.CreateUser(viewModel); // assert _userService.Verify(); }
public void CreateUser_displays_username_validation_error_when_duplicate_username_supplied() { // arrange var exception = new MembershipCreateUserException(MembershipCreateStatus.DuplicateUserName); _userService.Setup(x => x.Create(null, null, null, null, null)).Throws(exception); var viewModel = new CreateUserViewModel(); // act var returnedResult = _controller.CreateUser(viewModel); // assert var result = Assert.IsType<ViewResult>(returnedResult); Assert.Equal("", result.ViewName); Assert.Equal(viewModel, result.Model); var modelState = result.ViewData.ModelState; Assert.True(modelState.ContainsKey(string.Empty)); Assert.Equal(exception.Message, modelState[string.Empty].Errors.First().ErrorMessage); }
public ActionResult CreateUser(CreateUserViewModel createUserViewModel) { try { if (ModelState.IsValid) { var user = _userService.Create(createUserViewModel.Username, createUserViewModel.Password, createUserViewModel.Email, createUserViewModel.PasswordQuestion, createUserViewModel.PasswordAnswer); return RedirectToAction("Details", new {id = user.ProviderUserKey}); } } catch(MembershipCreateUserException e) { ModelState.AddModelError(string.Empty, e.Message); } return View(createUserViewModel); }