public void Details_LoggedInAdmin_ShouldReturnAPrimaryGuardian() { // Arrange Mock<IAccountService> accountService = new Mock<IAccountService>(); Mock<IRepositoryService> repoService = new Mock<IRepositoryService>(); Mock<IPrimaryGuardianRepository> primaryGuardianRepo = new Mock<IPrimaryGuardianRepository>(); Mock<ICenterRepository> centerRepo = new Mock<ICenterRepository>(); // center id 1 accountService.Setup(a => a.GetCurrentUserCenterId()).Returns(() => 1); accountService.Setup(a => a.GetRolesForUser()).Returns(new string[] { "Administrators" }); var pgs1 = new List<PrimaryGuardian>(); for (int i = 0; i < 15; i++) { pgs1.Insert(i, (Util.RandomPrimaryGuardian(i + 1, 1))); } var pgs1Children = new[] { Util.RandomChild(1, pgs1[0].Id), Util.RandomChild(2, pgs1[0].Id) }; var pgs1SecGuardian = new[] { Util.RandomSecondaryGuardian(1, pgs1[0].Id) }; primaryGuardianRepo.Setup(r => r.FindByIdAndCenterId(7, 1)).Returns(pgs1[6]); repoService.SetupGet(r => r.primaryGuardianRepo).Returns(() => primaryGuardianRepo.Object); repoService.SetupGet(r => r.centerRepo).Returns(() => centerRepo.Object); PrimaryGuardiansController controller = new PrimaryGuardiansController(accountService.Object, repoService.Object); // Act ActionResult result = controller.Details(7) as ActionResult; // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult asViewResult = (ViewResult)result; Assert.IsTrue(asViewResult.ViewBag.IsAdmin); Assert.IsInstanceOfType(asViewResult.ViewData.Model, typeof(PrimaryGuardian)); Assert.AreEqual(pgs1[6], (PrimaryGuardian)asViewResult.Model); }
public void Index_OrderByPostalCodePrefix() { // Arrange Mock<IAccountService> accountService = new Mock<IAccountService>(); Mock<IRepositoryService> repoService = new Mock<IRepositoryService>(); Mock<IPrimaryGuardianRepository> primaryGuardianRepo = new Mock<IPrimaryGuardianRepository>(); Mock<ICenterRepository> centerRepo = new Mock<ICenterRepository>(); // center id 1 accountService.Setup(a => a.GetCurrentUserCenterId()).Returns(() => 1); var pgs1 = new[] { Util.RandomPrimaryGuardian(1, 1), Util.RandomPrimaryGuardian(2, 2), Util.RandomPrimaryGuardian(3, 1) }; var pgs1Children = new[] { Util.RandomChild(1, pgs1[0].Id), Util.RandomChild(2, pgs1[0].Id) }; var pgs1SecGuardian = new[] { Util.RandomSecondaryGuardian(1, pgs1[0].Id) }; primaryGuardianRepo.Setup(r => r.FindAllWithCenterId(1)).Returns(pgs1); repoService.SetupGet(r => r.primaryGuardianRepo).Returns(() => primaryGuardianRepo.Object); repoService.SetupGet(r => r.centerRepo).Returns(() => centerRepo.Object); var request = new Mock<HttpRequestBase>(); request.SetupGet(x => x.HttpMethod).Returns("POST"); var controllerContext = new Mock<HttpContextBase>(); controllerContext.SetupGet(x => x.Request).Returns(request.Object); PrimaryGuardiansController controller = new PrimaryGuardiansController(accountService.Object, repoService.Object); controller.ControllerContext = new ControllerContext(controllerContext.Object, new RouteData(), controller); // Act string order = "Name desc5"; ActionResult result = controller.Index(order, null, null, null) as ActionResult; // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult asViewResult = (ViewResult)result; Assert.AreEqual(order, asViewResult.ViewBag.CurrentSort); Assert.IsInstanceOfType(asViewResult.ViewData.Model, typeof(IEnumerable<PrimaryGuardian>)); CollectionAssert.AreEqual(pgs1.OrderBy(p => p.PostalCodePrefix).ToList(), ((IEnumerable<PrimaryGuardian>)asViewResult.Model).ToList()); }
public void Index_LoggedInAndDifferentCenterId() { // Arrange Mock<IAccountService> accountService = new Mock<IAccountService>(); Mock<IRepositoryService> repoService = new Mock<IRepositoryService>(); Mock<IPrimaryGuardianRepository> primaryGuardianRepo = new Mock<IPrimaryGuardianRepository>(); Mock<ICenterRepository> centerRepo = new Mock<ICenterRepository>(); // center id 2 accountService.Setup(a => a.GetCurrentUserCenterId()).Returns(() => 2); var pgs1 = new[] { new PrimaryGuardian { Id = 1, CenterId = 1 }, new PrimaryGuardian { Id = 2, CenterId = 1, FirstName = "Anne" } }; var pgs2 = new[] { new PrimaryGuardian { Id = 3, CenterId = 2, FirstName = "John" } }; var pgs1Children = new[] { new Child { Id = 1, PrimaryGuardianId = pgs1[0].Id }, new Child { Id = 2, PrimaryGuardianId = pgs1[0].Id } }; var pgs2Children = new[] { new Child { Id = 3, PrimaryGuardianId = pgs2[0].Id, FirstName = "Johnny" } }; var pgs1SecGuardian = new[] { new SecondaryGuardian { Id = 1, PrimaryGuardianId = pgs1[0].Id } }; pgs1[0].SecondaryGuardians = pgs1SecGuardian; pgs1[0].Children = pgs1Children; pgs2[0].Children = pgs2Children; primaryGuardianRepo.Setup(r => r.FindAllWithCenterId(1)).Returns(pgs1); primaryGuardianRepo.Setup(r => r.FindAllWithCenterId(2)).Returns(pgs2); repoService.SetupGet(r => r.primaryGuardianRepo).Returns(() => primaryGuardianRepo.Object); repoService.SetupGet(r => r.centerRepo).Returns(() => centerRepo.Object); var request = new Mock<HttpRequestBase>(); request.SetupGet(x => x.HttpMethod).Returns("GET"); var controllerContext = new Mock<HttpContextBase>(); controllerContext.SetupGet(x => x.Request).Returns(request.Object); PrimaryGuardiansController controller = new PrimaryGuardiansController(accountService.Object, repoService.Object); controller.ControllerContext = new ControllerContext(controllerContext.Object, new RouteData(), controller); // Act ActionResult result = controller.Index(null, null, null, null) as ActionResult; // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult asViewResult = (ViewResult)result; Assert.IsInstanceOfType(asViewResult.ViewData.Model, typeof(IEnumerable<PrimaryGuardian>)); CollectionAssert.AreEquivalent(pgs2.ToList(), ((IEnumerable<PrimaryGuardian>)asViewResult.Model).ToList()); }
public void Index_FirstPageContainsFirst10Items() { // Arrange Mock<IAccountService> accountService = new Mock<IAccountService>(); Mock<IRepositoryService> repoService = new Mock<IRepositoryService>(); Mock<IPrimaryGuardianRepository> primaryGuardianRepo = new Mock<IPrimaryGuardianRepository>(); Mock<ICenterRepository> centerRepo = new Mock<ICenterRepository>(); // center id 1 accountService.Setup(a => a.GetCurrentUserCenterId()).Returns(() => 1); var pgs1 = new List<PrimaryGuardian>(); for (int i = 0; i < 15; i++) { pgs1.Insert(i, (Util.RandomPrimaryGuardian(i+1, 1))); } var pgs1Children = new[] { Util.RandomChild(1, pgs1[0].Id), Util.RandomChild(2, pgs1[0].Id) }; var pgs1SecGuardian = new[] { Util.RandomSecondaryGuardian(1, pgs1[0].Id) }; primaryGuardianRepo.Setup(r => r.FindAllWithCenterId(1)).Returns(pgs1); repoService.SetupGet(r => r.primaryGuardianRepo).Returns(() => primaryGuardianRepo.Object); repoService.SetupGet(r => r.centerRepo).Returns(() => centerRepo.Object); var request = new Mock<HttpRequestBase>(); request.SetupGet(x => x.HttpMethod).Returns("POST"); var controllerContext = new Mock<HttpContextBase>(); controllerContext.SetupGet(x => x.Request).Returns(request.Object); PrimaryGuardiansController controller = new PrimaryGuardiansController(accountService.Object, repoService.Object); controller.ControllerContext = new ControllerContext(controllerContext.Object, new RouteData(), controller); // Act ActionResult result = controller.Index(null, null, null, null) as ActionResult; // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult asViewResult = (ViewResult)result; Assert.IsInstanceOfType(asViewResult.ViewData.Model, typeof(IEnumerable<PrimaryGuardian>)); int j = 0; CollectionAssert.AreEqual(pgs1.OrderBy(p => p.LastName).TakeWhile(p => { bool have10 = j < 10; j++; return have10; }).ToList(), ((IEnumerable<PrimaryGuardian>)asViewResult.Model).ToList()); }
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"); }
public void Index_WithSearchString() { // Arrange Mock<IAccountService> accountService = new Mock<IAccountService>(); Mock<IRepositoryService> repoService = new Mock<IRepositoryService>(); Mock<IPrimaryGuardianRepository> primaryGuardianRepo = new Mock<IPrimaryGuardianRepository>(); Mock<ICenterRepository> centerRepo = new Mock<ICenterRepository>(); // center id 1 accountService.Setup(a => a.GetCurrentUserCenterId()).Returns(() => 1); var pgs1 = new[] { new PrimaryGuardian { Id = 1, CenterId = 1, FirstName = "John" }, new PrimaryGuardian { Id = 2, CenterId = 1, FirstName = "Anne" } }; var pgs1Children = new[] { new Child { Id = 1, PrimaryGuardianId = pgs1[0].Id }, new Child { Id = 2, PrimaryGuardianId = pgs1[0].Id } }; var pgs1SecGuardian = new[] { new SecondaryGuardian { Id = 1, PrimaryGuardianId = pgs1[0].Id } }; pgs1[0].SecondaryGuardians = pgs1SecGuardian; pgs1[0].Children = pgs1Children; primaryGuardianRepo.Setup(r => r.FindAllWithCenterId(1)).Returns(pgs1); repoService.SetupGet(r => r.primaryGuardianRepo).Returns(() => primaryGuardianRepo.Object); repoService.SetupGet(r => r.centerRepo).Returns(() => centerRepo.Object); var request = new Mock<HttpRequestBase>(); request.SetupGet(x => x.HttpMethod).Returns("POST"); var controllerContext = new Mock<HttpContextBase>(); controllerContext.SetupGet(x => x.Request).Returns(request.Object); PrimaryGuardiansController controller = new PrimaryGuardiansController(accountService.Object, repoService.Object); controller.ControllerContext = new ControllerContext(controllerContext.Object, new RouteData(), controller); // Act string searchString = "john"; ActionResult result = controller.Index(null, null, searchString, null) as ActionResult; // Assert Assert.IsInstanceOfType(result, typeof(ViewResult)); ViewResult asViewResult = (ViewResult)result; Assert.AreEqual(searchString, asViewResult.ViewBag.CurrentFilter); Assert.IsInstanceOfType(asViewResult.ViewData.Model, typeof(IEnumerable<PrimaryGuardian>)); // searched by john, but search is case-insensitive so expect to return only John CollectionAssert.AreEquivalent(pgs1.Where(p => p.FirstName == "John").ToList(), ((IEnumerable<PrimaryGuardian>)asViewResult.Model).ToList()); }