public void Create_TestFormValidation() { var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userProfileServiceMock = new Mock<IUserProfileService>(); var userProfileService = userProfileServiceMock.Object; var userController = new UserController(userService, userProfileService); userController.ViewData.ModelState.AddModelError("rofl", "nao"); //This simulates any validation error var createModel = new CreateViewModel(); var userCredentials = new UserCredentials() { Email = createModel.Email, Password = createModel.Password }; var result = userController.Index(createModel) as ViewResult; //Verify that the controller does not try to call login on IUserService userServiceMock.Verify(a => a.Create(userCredentials), Times.Never()); Assert.IsNotNull(result, "Login Action did not yield a ViewResult"); Assert.AreSame(createModel, result.Model, "Controller did not forward the defective view"); }
public void Create_TestExistingUser() { var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userProfileServiceMock = new Mock<IUserProfileService>(); var userProfileService = userProfileServiceMock.Object; var userController = new UserController(userService, userProfileService); var createModel = new CreateViewModel { Username = "******", Password = "******", StreetAddress = "rofl", City = "mao", ZipCode = 1000 }; var credentials = new UserCredentials() { Email = createModel.Email, Password = createModel.Password }; userServiceMock.Setup(a => a.Create(credentials)).Throws(new UserExistsException()); var viewResult = userController.Index(createModel) as ViewResult; Assert.IsNotNull(viewResult, "Controller did not return a ViewResult"); Assert.AreSame(createModel, viewResult.Model); userServiceMock.Verify(a => a.Create(credentials)); }
public void Create_TestSuccess() { var userServiceMock = new Mock<IUserService>(); var userService = userServiceMock.Object; var userProfileServiceMock = new Mock<IUserProfileService>(); var userProfileService = userProfileServiceMock.Object; var controllerContextMock = new Mock<ControllerContext>(); var userController = new UserController(userService, userProfileService); userController.ControllerContext = controllerContextMock.Object; var createModel = new CreateViewModel { Username = "******", Password = "******", StreetAddress = "rofl", City = "mao", ZipCode = 1000 }; var credentials = new UserCredentials() { Email = createModel.Email, Password = createModel.Password }; var accessToken = new UserAccessToken("123456"); userServiceMock.Setup(a => a.Create(credentials)).Returns(accessToken); userServiceMock.Setup(a => a.Login(credentials)).Returns(accessToken); var viewResult = userController.Index(createModel) as RedirectToRouteResult; Assert.IsNotNull(viewResult, "Controller did not return a RedirectToRouteResult"); var userProfile = new UserProfile() { Username = createModel.Username, Address = new Address() { Street = createModel.StreetAddress, City = createModel.City, ZipCode = createModel.ZipCode } }; userServiceMock.Verify(a => a.Create(credentials)); userProfileServiceMock.Verify(a => a.UpdateProfile(accessToken, userProfile)); }