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 Cannot_Edit_Nonexistent_Contact()
        {
            //arrange
            Mock<IContactRepository> mock = new Mock<IContactRepository>();
            mock.Setup(m => m.Contacts).Returns(new Contact[]
            {
                new Contact {ContactId = 1, LastName = "1"},
                new Contact {ContactId = 2, LastName = "2"},
                new Contact {ContactId = 3, LastName = "3"},
                new Contact {ContactId = 4, LastName = "4"},
                new Contact {ContactId = 5, LastName = "5"}
            }.AsQueryable());
            ContactController controller = new ContactController(mock.Object);

            //act
            Contact result = (Contact)controller.Edit(6).ViewData.Model;

            //assert
            Assert.IsNull(result);
        }
        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));
        }
        public void Can_Edit_Contact()
        {
            //arrange
            Mock<IContactRepository> mock = new Mock<IContactRepository>();
            mock.Setup(m => m.Contacts).Returns(new Contact[]
            {
                new Contact {ContactId = 1, LastName = "1"},
                new Contact {ContactId = 2, LastName = "2"},
                new Contact {ContactId = 3, LastName = "3"},
                new Contact {ContactId = 4, LastName = "4"},
                new Contact {ContactId = 5, LastName = "5"}
            }.AsQueryable());
            ContactController controller = new ContactController(mock.Object);

            //act
            Contact c1 = controller.Edit(1).ViewData.Model as Contact;
            Contact c2 = controller.Edit(3).ViewData.Model as Contact;
            Contact c3 = controller.Edit(5).ViewData.Model as Contact;

            //assert
            Assert.AreEqual(1, c1.ContactId);
            Assert.AreEqual(3, c2.ContactId);
            Assert.AreEqual(5, c3.ContactId);
        }