public ActionResult EditFamily(Family family) { if (ModelState.IsValid) { families.SaveFamily(family); TempData["message"] = string.Format("The {0} family has been saved.", family.FamilyName); return RedirectToAction("Index"); } else { // model is invalid, some input data errors exist return View(family); } }
public void SaveFamily(Family family) { if (0 == family.Id) { context.Families.Add(family); } else { Family dbEntry = context.Families.Find(family.Id); if (dbEntry!=null) { dbEntry.Description = family.Description; dbEntry.FamilyName = family.FamilyName; } } context.SaveChanges(); }
public void CanDeleteFamilyId() { // Arrange // need extra Moq functionality so can't use the normal mock repo Family family = new Family{Id=2, FamilyName="Deer", Description="The Deer Family"}; Mock<IFamilyRepository> mock = new Mock<IFamilyRepository>(); mock.Setup(m => m.Families).Returns(new List<Family> { new Family{Id=1, FamilyName="Doe", Description="The Doe Family"}, family, new Family{Id=3, FamilyName="Doey", Description="The Doey Family"} }); AdminController target = new AdminController(GetPersonRepository(), mock.Object); // Act target.DeleteFamily(family.Id); // Assertion // assert that the delete functionality was called with the correct ID mock.Verify(m => m.DeleteFamily(family.Id)); }
public void CannotSaveInvalidFamilyChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IFamilyRepository> mock = new Mock<IFamilyRepository>(); AdminController target = new AdminController(GetPersonRepository(), mock.Object); Family family = new Family { FamilyName = "Test", Description = "The Test Family" }; // add a deliberate error to the model target.ModelState.AddModelError("error", "error"); // Act ActionResult result = target.EditFamily(family); // Assertion - save method was NOT called, and user WAS sent back to the family view mock.Verify(m => m.SaveFamily(It.IsAny<Family>()), Times.Never()); Assert.IsInstanceOfType(result, typeof(ViewResult)); }
public void CanSaveValidFamilyChanges() { // Arrange // need extra Moq functionality so can't use the normal mock repo, but this isn't dependant on content anyway Mock<IFamilyRepository> mock = new Mock<IFamilyRepository>(); AdminController target = new AdminController(GetPersonRepository(), mock.Object); Family family = new Family { FamilyName = "Test", Description = "The Test Family" }; // Act ActionResult result = target.EditFamily(family); // Assertion - save method was called, and user was not sent back to the family view mock.Verify(m => m.SaveFamily(family)); Assert.IsNotInstanceOfType(result, typeof(ViewResult)); }