public ActionResult CreateUser(CreateUserViewModel createUserViewModel)
        {
            if (!ModelState.IsValid)
                return View(createUserViewModel);

            try
            {
                if(createUserViewModel.Password != createUserViewModel.ConfirmPassword)
                    throw new MembershipCreateUserException("Passwords do not match.");

                var user = _userService.Create(
                    createUserViewModel.Username,
                    createUserViewModel.Password,
                    createUserViewModel.Email,
                    createUserViewModel.PasswordQuestion,
                    createUserViewModel.PasswordAnswer,
                    true);

                if (createUserViewModel.InitialRoles != null)
                {
                    var rolesToAddUserTo = createUserViewModel.InitialRoles.Where(x => x.Value).Select(x => x.Key);
                    foreach (var role in rolesToAddUserTo)
                        _rolesService.AddToRole(user, role);
                }

                return RedirectToAction("Details", new { id = user.ProviderUserKey });
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError(string.Empty, e.Message);
                return View(createUserViewModel);
            }
        }
		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, true)).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 ViewResult CreateUser()
 {
     var model = new CreateUserViewModel
                     {
                         InitialRoles = _rolesService.FindAll().ToDictionary(k => k, v => false)
                     };
     return View(model);
 }
		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!";
			const bool isApproved = true;
			_userService.Setup(x => x.Create(username, password, email, passwordQuestion, passwordAnswer, isApproved)).Returns(user).Verifiable();

			// act
			var viewModel = new CreateUserViewModel
			{
				Username = username,
				Password = password,
				ConfirmPassword = password,
				Email = email,
				PasswordQuestion = passwordQuestion,
				PasswordAnswer = passwordAnswer
			};
			_controller.CreateUser(viewModel);

			// assert
			_userService.Verify();
		}