예제 #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);
        }
예제 #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();
 }
예제 #3
0
파일: UnitTest1.cs 프로젝트: Junch/CSharp
        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));
        }
예제 #4
0
파일: UnitTest1.cs 프로젝트: Junch/CSharp
        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));
        }