public void TestRegisterPost_MembershipCreateUserExceptionIsThrown_ReturnsErrorInModelState() { // Arrange AccountController controller = new AccountController(_shoppingCartFactory, _securityProvider, _session); var model = new RegisterModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; //_securityProvider.WhenForAnyArgs(c => c.CreateUserAndAccount("", "")).Do(a => { throw new MembershipCreateUserException(); }); _securityProvider.When(c => c.CreateUserAndAccount(Arg.Any<string>(), Arg.Any<string>())).Do(a => { throw new MembershipCreateUserException(); }); controller.ViewData.ModelState.Clear(); // Act ActionResult result = controller.CallWithModelValidation(c => c.Register(model), model); // Assert // Make sure that our validation found the error! Assert.IsTrue(controller.ViewData.ModelState.Count == 1, "FirstName must be required."); }
public void TestRegisterPost_InvalidModel_ReturnsView() { // Arrange AccountController controller = new AccountController(_shoppingCartFactory, _securityProvider, _session); var model = new RegisterModel() { UserName = "", Password = "", ConfirmPassword = "******" }; // Act //ActionResult result = controller.Register(model); //This will not call model validation within the controller //We use the CallWithModelValidation extension method ActionResult result = controller.CallWithModelValidation(c => c.Register(model), model); // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); var viewResult = (ViewResult)result; Assert.AreEqual(viewResult.Model, model); }
public void TestRegisterPost_ValidUser_CreatesUserAndLogsIn() { // Arrange AccountController controller = new AccountController(_shoppingCartFactory, _securityProvider, _session); var model = new RegisterModel() { UserName = "******", Password = "******", ConfirmPassword = "******" }; // Act ActionResult result = controller.CallWithModelValidation(c => c.Register(model), model); // Assert _securityProvider.Received().CreateUserAndAccount(model.UserName, model.Password); _securityProvider.Received().Login(model.UserName, model.Password); }