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"); }
private void detach_KeyContacts(KeyContact entity) { this.SendPropertyChanging(); entity.Client = null; }
private void attach_KeyContacts(KeyContact entity) { this.SendPropertyChanging(); entity.Client = this; }
partial void DeleteKeyContact(KeyContact instance);
partial void UpdateKeyContact(KeyContact instance);
partial void InsertKeyContact(KeyContact instance);
private void detach_KeyContacts(KeyContact entity) { this.SendPropertyChanging(); entity.Relationship = null; }
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_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."); }
public void SaveKeyContact(KeyContact keyContact) { // if its a new client, insert it if (keyContact.KeyContactID == 0) { _db.KeyContacts.InsertOnSubmit(keyContact); } else if (_db.KeyContacts.GetOriginalEntityState(keyContact) == null) { // we are updating an existing key contact, but it's not attached // to the data context, so attach it and detect changes // todo: address logic should go in SaveAddress() function _db.Addresses.Attach(keyContact.Address); _db.KeyContacts.Attach(keyContact); _db.KeyContacts.Context.Refresh(RefreshMode.KeepCurrentValues, keyContact); _db.Addresses.Context.Refresh(RefreshMode.KeepCurrentValues, keyContact.Address); } _db.SubmitChanges(); }
public void DeleteKeyContact(KeyContact keyContact) { _db.Addresses.DeleteOnSubmit(keyContact.Address); _db.KeyContacts.DeleteOnSubmit(keyContact); _db.KeyContacts.Context.SubmitChanges(); }