public void SaveContact(Contact contact)
 {
     if (contact.ContactId == 0)
     {
         context.Contacts.Add(contact);
     }
     else
     {
         Contact dbEntry = context.Contacts.Find(contact.ContactId);
         if (dbEntry != null)
         {
             dbEntry.LastName = contact.LastName;
             dbEntry.FirstName = contact.FirstName;
             dbEntry.City = contact.City;
             dbEntry.Function = contact.Function;
             dbEntry.WorkNumber = contact.WorkNumber;
             dbEntry.WorkEmail = contact.WorkEmail;
             dbEntry.WorkAdress = contact.WorkAdress;
             dbEntry.MobileNumber = contact.MobileNumber;
             dbEntry.HomeNumber = contact.HomeNumber;
             dbEntry.PersonalEmail = contact.PersonalEmail;
             dbEntry.PersonalLink = contact.PersonalLink;
         }
     }
     context.SaveChanges();
 }
        public ActionResult Edit(Contact contact)
        {
            if (ModelState.IsValid)
            {
                // TODO: Add update logic here
                repository.SaveContact(contact);
                TempData["message"] = string.Format("{0} {1} {2} в {3} был сохранен", contact.Function, contact.LastName, contact.FirstName, contact.City);

                return RedirectToAction("ListAllForCRUD");
            }
            else
            {
                return View(contact);
            }
        }
        public void Cannot_Save_Invalid_Changes()
        {
            //arrange
            Mock<IContactRepository> mock = new Mock<IContactRepository>();
            ContactController controller = new ContactController(mock.Object);
            Contact contact = new Contact { LastName = "Test" };
            controller.ModelState.AddModelError("error", "error");

            //act
            ActionResult result = controller.Edit(contact);

            //assert
            mock.Verify(c => c.SaveContact(It.IsAny<Contact>()), Times.Never());
            Assert.IsInstanceOfType(result, typeof(ViewResult));
        }
        public void Can_Delete_Valid_Contact()
        {
            //arrange
            Contact contact = new Contact { ContactId = 2, LastName = "2" };
            Mock<IContactRepository> mock = new Mock<IContactRepository>();
            mock.Setup(m => m.Contacts).Returns(new Contact[]
            {
                new Contact {ContactId = 1, LastName = "1"},
                contact,
                new Contact {ContactId = 3, LastName = "3"},
            }.AsQueryable());
            ContactController controller = new ContactController(mock.Object);

            //act
            controller.Delete(contact.ContactId);

            //assert
            mock.Verify(m => m.DeleteContact(contact.ContactId));
        }
        public void Can_Save_Valid_Changes()
        {
            //arrange
            Mock<IContactRepository> mock = new Mock<IContactRepository>();
            ContactController controller = new ContactController(mock.Object);
            Contact contact = new Contact { LastName = "Test" };

            //act
            ActionResult result = controller.Edit(contact);

            //assert
            mock.Verify(c => c.SaveContact(contact));
            Assert.IsNotInstanceOfType(result, typeof(ViewResult));
        }