Exemplo n.º 1
0
        public ActionResult Edit(User user)
        {
            if (ModelState.IsValid) {
                repository.SaveUser(user);
                TempData["message"] = string.Format("{0} has been saved", user.Name);
                return RedirectToAction("List");
            }

            return View(user);
        }
Exemplo n.º 2
0
 public void SaveUser(User user)
 {
     if (user.UserId == 0) {
         context.Users.Add(user);
     } else {
         User dbEntry = context.Users.Find(user.UserId);
         if (dbEntry != null) {
             dbEntry.Name = user.Name;
             dbEntry.Email = user.Email;
         }
     }
     context.SaveChanges();
 }
Exemplo n.º 3
0
        public void Can_Save_valid_User()
        {
            // Arrange
            Mock<IUserRepository> mock = new Mock<IUserRepository>();
            UserController target = new UserController(mock.Object);
            User usr = new User { Name = "Test" };

            // Act
            ActionResult result = target.Edit(usr);

            // Assert - check that the repository was called
            mock.Verify(m => m.SaveUser(usr));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }
Exemplo n.º 4
0
        public void Cannot_Save_Invalid_Changes()
        {
            // Arrange - create mock repository
            Mock<IUserRepository> mock = new Mock<IUserRepository>();
            // Arrange - create the controller
            UserController target = new UserController(mock.Object);
            // Arrange - create a product
            User usr = new User { Name = "Test" };
            // Arrange - add an error to the model state
            target.ModelState.AddModelError("error", "error");

            // Act - try to save the product
            ActionResult result = target.Edit(usr);

            // Assert - check that the repository was not called
            mock.Verify(m => m.SaveUser(It.IsAny<User>()), Times.Never());
            // Assert - check the method result type
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }