Example #1
0
        public void Cannot_Save_Invalid_Edited_Client()
        {
            // ARRANGE: given a repository and an existing key contact...
            var mockRepository = new Mock<IClientRepository>();

            KeyContact[] existingKeyContact = new KeyContact[]
            {
                new KeyContact { KeyContactID = 1, FirstName = "Bill", LastName = "Cowher" }
            };

            // create data to change key contact to
            mockRepository.Setup(x => x.KeyContacts).Returns(existingKeyContact.AsQueryable());
            var keyContactForm = new KeyContactFormViewModel { KeyContactID = 1, FirstName = "Mike", LastName = "Tomlin" };

            // ACT: ...when a user tries to save changes to the invalid key contact...
            var controller = new KeyContactsController(mockRepository.Object);
            controller.ModelState.AddModelError("SomeProperty", "Got invalid data");
            var result = controller.Edit(keyContactForm) as ViewResult;

            // ASSERT: then the values of the key contact are not changed and the edit page is redisplayed
            Assert.NotNull(result);
            var clientFormResult = (KeyContactFormViewModel)result.ViewData.Model;
            clientFormResult.FirstName.ShouldEqual("Mike");
            clientFormResult.LastName.ShouldEqual("Tomlin");
            result.ViewName.ShouldEqual("Edit");
        }
Example #2
0
        public void Can_Save_Valid_Edited_KeyContact()
        {
            // ARRANGE: given a repository and an existing key contact...
            var mockRepository = new Mock<IClientRepository>();

            KeyContact[] existingKeyContact = new KeyContact[]
            {
                new KeyContact { KeyContactID = 1, FirstName = "Bill", LastName = "Cowher" }
            };

            // create data to change key contact to
            mockRepository.Setup(x => x.KeyContacts).Returns(existingKeyContact.AsQueryable());
            var keyContactForm = new KeyContactFormViewModel { KeyContactID = 1, FirstName = "Mike", LastName = "Tomlin" };

            // ACT: ...when a user tries to save changes to the key contact...
            var controller = new KeyContactsController(mockRepository.Object);
            var result = controller.Edit(keyContactForm);

            // ASSERT: then the key contact is saved, user is redirected to the detail page,
            //         values are changed, and a confirmation message is displayed
            result.ShouldBeRedirectionTo(new { action = "Detail" });
            controller.TempData["message"].ShouldEqual("Key Contact Mike Tomlin has been saved.");
        }