public static Child RandomChild(int Id, int primaryGuardianId) { Child c = new Child { Id = Id, FirstName = Probability(0.8) ? RandomString(3) : null, LastName = Probability(0.8) ? RandomString(3) : null, Allergies = Probability(0.8) ? RandomString(3) : null, Birthdate = RandomDate(), DateCreated = RandomDate(), PrimaryGuardianId = primaryGuardianId }; return c; }
public void Update(Child c) { db.Entry(c).State = EntityState.Modified; db.SaveChanges(); }
public void Delete(Child c) { db.Children.Remove(c); db.SaveChanges(); }
public void Add(Child c) { db.Children.Add(c); db.SaveChanges(); }
public void EditPost_WithLoggedInStaff_ShouldReturnAPrimaryGuardian() { // Arrange Mock<IAccountService> accountService = new Mock<IAccountService>(); Mock<IRepositoryService> repoService = new Mock<IRepositoryService>(); Mock<IPrimaryGuardianRepository> primaryGuardianRepo = new Mock<IPrimaryGuardianRepository>(); Mock<IChildRepository> childRepo = new Mock<IChildRepository>(); Mock<ISecondaryGuardianRepository> secondaryGuardianRepo = new Mock<ISecondaryGuardianRepository>(); Mock<ICenterRepository> centerRepo = new Mock<ICenterRepository>(); // center id 1 accountService.Setup(a => a.GetCurrentUserCenterId()).Returns(() => 1); accountService.Setup(a => a.GetRolesForUser()).Returns(new string[] { "Staff" }); var pgs1 = new List<PrimaryGuardian>(); var pgs1Children = new List<Child>(); var pgs1SecondaryGuardian = new List<SecondaryGuardian>(); for (int i = 0; i < 15; i++) { pgs1.Insert(i, (Util.RandomPrimaryGuardian(i + 1, 1))); } for (int j = 0; j < 5; j++) { Child child; SecondaryGuardian secGuardian; if (j == 3) { // null fields child = new Child(); secGuardian = new SecondaryGuardian(); } else { child = Util.RandomChild(j + 1, pgs1[0].Id); secGuardian = Util.RandomSecondaryGuardian(j + 1, pgs1[0].Id); // simulate deleted if (j == 2) { child.Delete = true; secGuardian.Delete = true; } // simulate newly created else if (j == 4) { child.Id = 0; secGuardian.Id = 0; } } pgs1Children.Insert(j, child); pgs1SecondaryGuardian.Insert(j, secGuardian); } pgs1[0].Children = pgs1Children; pgs1[0].SecondaryGuardians = pgs1SecondaryGuardian; // Deleted secondaryGuardianRepo.Setup(s => s.FindById(3)).Returns(pgs1SecondaryGuardian[2]); childRepo.Setup(s => s.FindById(3)).Returns(pgs1Children[2]); repoService.SetupGet(r => r.primaryGuardianRepo).Returns(() => primaryGuardianRepo.Object); repoService.SetupGet(r => r.centerRepo).Returns(() => centerRepo.Object); repoService.SetupGet(r => r.childRepo).Returns(() => childRepo.Object); repoService.SetupGet(r => r.secondaryGuardianRepo).Returns(() => secondaryGuardianRepo.Object); PrimaryGuardiansController controller = new PrimaryGuardiansController(accountService.Object, repoService.Object); // doplicate children and secondary guardians first so we can verify results later var pgs1Children_2 = pgs1Children.ToList(); var pgs1SecondaryGuardian_2 = pgs1SecondaryGuardian.ToList(); // Act ActionResult result = controller.Edit(pgs1[0]) as ActionResult; // Assert PrimaryGuardian pg = pgs1[0]; // Id 0 should be deleted after call to repo.Add // Id 4 should be deleted for all null fields // Id 3 should be deleted after call to repo.Delete List<Child> expectedChildren = pgs1Children_2.Where(c => c.Id != 0 && c.Id != 3 && c.Id != 4).ToList(); List<SecondaryGuardian> expectedSecondaryGuardians = pgs1SecondaryGuardian_2.Where(s => s.Id != 0 && s.Id != 3 && s.Id != 4).ToList(); CollectionAssert.AreEqual(expectedChildren, pgs1Children); CollectionAssert.AreEqual(expectedSecondaryGuardians, pgs1SecondaryGuardian); Assert.IsInstanceOfType(result, typeof(RedirectToRouteResult)); RedirectToRouteResult asRedirectResult = (RedirectToRouteResult)result; StringAssert.Equals(asRedirectResult.RouteValues["action"], "Index"); StringAssert.Equals(asRedirectResult.RouteValues["controller"], "PrimaryGuardians"); }