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"); }
// GET CREATE KEY CONTACT public ViewResult Create(int ClientID) { var relationships = clientsRepository.Relationships.ToList(); // create view model var viewModel = new KeyContactFormViewModel { Relationships = new SelectList(relationships, "RelationshipID", "RelationshipName"), clientId = ClientID }; return View("Edit", viewModel); }
public ActionResult Edit(KeyContactFormViewModel keyContactForm) { var keyContact = Mapper.Map<KeyContactFormViewModel, KeyContact>(keyContactForm); if (ModelState.IsValid) { clientsRepository.SaveKeyContact(keyContact); TempData["message"] = "Key Contact: " + keyContact.FirstName + " " + keyContact.LastName + " has been saved."; return RedirectToAction("Detail", "KeyContacts", new { keyContact.KeyContactID }); } else // validation error, so redisplay the same view keyContactForm.Relationships = new SelectList(clientsRepository.Relationships.ToList(), "RelationshipID", "RelationshipName", keyContactForm.RelationshipID); return View("Edit", keyContactForm); }
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_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."); }