public void SavePerson(Person person) { if (0== person.Id) { context.People.Add(person); } else { Person dbEntry = context.People.Find(person.Id); if (dbEntry != null) { dbEntry.BirthDay = person.BirthDay; dbEntry.BirthMonth = person.BirthMonth; dbEntry.BirthYear = person.BirthYear; dbEntry.FamilyId = person.FamilyId; dbEntry.FirstName = person.FirstName; dbEntry.LastName = person.LastName; dbEntry.MiddleName = person.MiddleName; dbEntry.NamePrefix = person.NamePrefix; dbEntry.NameSuffix = person.NameSuffix; dbEntry.ParentId = person.ParentId; dbEntry.ParentId2 = person.ParentId2; } } context.SaveChanges(); }
public ActionResult EditPerson(Person person) { if (ModelState.IsValid) { people.SavePerson(person); TempData["message"] = string.Format("{0} has been saved.", person.FirstName); return RedirectToAction("Index"); } else { // model is invalid, some input data errors exist return View(person); } }
public void CanDeletePersonId() { // Arrange // need extra Moq functionality so can't use the normal mock repo Person person = new Person{Id=2, FamilyId=1, ParentId=0, ParentId2=0, FirstName="Jane", LastName="Doe"}; Mock < IPersonRepository > mock = new Mock<IPersonRepository>(); mock.Setup(m => m.People).Returns(new List<Person> { new Person{Id=1, FamilyId=1, ParentId=0, ParentId2=0, FirstName="John", LastName="Doe"}, person, new Person{Id=3, FamilyId=1, ParentId=1, ParentId2=2, FirstName="Jennifer", LastName="Deer"} }); AdminController target = new AdminController(mock.Object, GetFamilyRepository()); // Act target.DeletePerson(person.Id); // Assertion // assert that the delete functionality was called with the correct ID mock.Verify(m => m.DeletePerson(person.Id)); }
public void CannotSaveInvalidPersonChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IPersonRepository> mock = new Mock<IPersonRepository>(); AdminController target = new AdminController(mock.Object, GetFamilyRepository()); Person person = new Person { FirstName = "Testable", LastName = "Test" }; // add a deliberate error to the model target.ModelState.AddModelError("error", "error"); // Act ActionResult result = target.EditPerson(person); // Assertion - save method was NOT called, and user WAS sent back to the family view mock.Verify(m => m.SavePerson(It.IsAny<Person>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void CanSaveValidPersonChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IPersonRepository> mock = new Mock<IPersonRepository>(); AdminController target = new AdminController(mock.Object,GetFamilyRepository()); Person person = new Person { FirstName = "Testable", LastName = "Test" }; // Act ActionResult result = target.EditPerson(person); // Assertion - save method was called, and user was not sent back to the family view mock.Verify(m => m.SavePerson(person)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }