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"); }
public void Cannot_Save_Invalid_New_KeyContact() { // ARRANGE: given a repository and a key contact to save... var mockRepository = new Mock<IClientRepository>(); // create key contact to save KeyContactFormViewModel keyContact = new KeyContactFormViewModel { FirstName = "Bill", LastName = "Cowher" }; // ACT: ...when a user tries to save the invalid key contact... var controller = new KeyContactsController(mockRepository.Object); controller.ModelState.AddModelError("SomeProperty", "Got invalid data"); var result = controller.Edit(keyContact) as ViewResult; // ASSERT: ...the key contact is not saved and the edit page is redisplayed Assert.NotNull(result); var keyContactFormResult = (KeyContactFormViewModel)result.ViewData.Model; keyContactFormResult.FirstName.ShouldEqual("Bill"); keyContactFormResult.LastName.ShouldEqual("Cowher"); result.ViewName.ShouldEqual("Edit"); }
public void Can_Delete_KeyContact() { // ARRANGE: given a repository containing a key contact... // put a key contact in the repo var mockRepository = new Mock<IClientRepository>(); var keyContact = new KeyContact { KeyContactID = 100, FirstName = "Sonic", LastName = "TheHedgehog" }; mockRepository.Setup(x => x.GetKeyContact(100)).Returns(keyContact); // ACT: ...and user tries to delete that key contact var controller = new KeyContactsController(mockRepository.Object); var result = controller.Delete(100); // ASSERT: ... it's deleted and user sees confirmation mockRepository.Verify(x => x.DeleteKeyContact(keyContact)); Assert.NotNull(result); result.ShouldBeRedirectionTo(new { action = "Detail" }); controller.TempData["message"].ShouldEqual("Key Contact Sonic TheHedgehog was successfully deleted."); }
public void Can_Save_Valid_New_KeyContact() { // ARRANGE: given a repository and a key contact to save... var mockRepository = new Mock<IClientRepository>(); // create key contact to save KeyContactFormViewModel keyContact = new KeyContactFormViewModel { FirstName = "Bill", LastName = "Cowher" }; // ACT: ... when a user tries to save the valid key contact var controller = new KeyContactsController(mockRepository.Object); var result = controller.Edit(keyContact); // ASSERT: then the key contact is saved, user is redirected to the detail page, // and a confirmation message is displayed result.ShouldBeRedirectionTo(new { action = "Detail" }); controller.TempData["message"].ShouldEqual("Key Contact Bill Cowher has been saved."); }
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."); }