public void ShouldNotAssignPermitAsEmployeeAlreadyHasPermit() { //Arrange string wvuEmployeeID = "001"; Lot lot = new Lot("999", "Test Name", "Test Address", 10); lot.LotID = 100; lot.CurrentOccupancy = 8; int lotTypeID = 3; DateTime startDate = new DateTime(2020, 10, 5); int expectedCurrentlyOccupiedSpotsAfterAssignment = 8; //Does chosen employee already have permit? True mockPermitRepo.Setup(m => m.DoesWVUEmployeeHavePermit(wvuEmployeeID)).Returns(true); //Is the chosen lot available? mockLotRepo.Setup(l => l.IsChosenLotAvailable(lot.LotID)).Returns(true); //If both conditions are met then assign permit mockLotStatusRepo.Setup(s => s.FindPermitAmount(lot.LotID, lotTypeID)).Returns(500.00); //IF an object is created in controller method we can get access to that object using Callback on the mock setup Permit permit = null; mockPermitRepo.Setup(p => p.AddPermit(It.IsAny <Permit>())).Returns(Task.CompletedTask).Callback <Permit>(p => permit = p); mockLotRepo.Setup(l => l.FindLot(lot.LotID)).Returns(lot); AssignPermitViewModel viewModel = new AssignPermitViewModel(); viewModel.WVUEmployeeID = wvuEmployeeID; viewModel.LotID = lot.LotID; viewModel.StartDate = startDate; string expectedErrorMessage = "Employee already has a permit"; // double expectedPermitAmount = 500; mockLotRepo.Setup(l => l.ListAllLots()).Returns(new List <Lot>()); mockApplicationUserRepo.Setup(a => a.ListAllWVUEmployees()).Returns(new List <WVUEmployee>()); //Act controller.AssignPermit(viewModel); //Assert Assert.Equal(expectedCurrentlyOccupiedSpotsAfterAssignment, lot.CurrentOccupancy); Assert.Equal(expectedErrorMessage, controller.ModelState["EmployeePermit"].Errors[0].ErrorMessage); // Assert.Equal(expectedPermitAmount, permit.PermitAmount); }
public IActionResult AssignPermit(AssignPermitViewModel viewModel) { string parkingEmployeeID = iApplicationUserRepo.FindUserID(); //= User.FindFirst(ClaimTypes.NameIdentifier).Value; //bool assignPermit; string errorMessage = "None"; bool employeeHasPermit = iPermitRepo.DoesWVUEmployeeHavePermit(viewModel.WVUEmployeeID); if (employeeHasPermit) { errorMessage = "Employee already has a permit"; ModelState.AddModelError("EmployeePermit", errorMessage); } bool lotAvailable = iLotRepo.IsChosenLotAvailable(viewModel.LotID); if (!lotAvailable) { errorMessage = "Lot is not available"; ModelState.AddModelError("LotAvailable", errorMessage); } if (!employeeHasPermit && lotAvailable) { int lotTypeID = 3; double permitAmount = iLotStatusRepo.FindPermitAmount(viewModel.LotID, lotTypeID); //DateTime startDate = DateTime.Today.Date; DateTime endDate = viewModel.StartDate.AddYears(1); Permit permit = new Permit(permitAmount, viewModel.StartDate, endDate, viewModel.WVUEmployeeID, parkingEmployeeID); iPermitRepo.AddPermit(permit).Wait(); //lot CurrentOccupancy to +1 Lot lot = iLotRepo.FindLot(viewModel.LotID); lot.CurrentOccupancy += 1; iLotRepo.EditLot(lot).Wait(); return(RedirectToAction("ListAllPermits")); } else { ViewData["Lots"] = new SelectList(iLotRepo.ListAllLots(), "LotID", "LocationName"); ViewData["WVUEmployees"] = new SelectList(iApplicationUserRepo.ListAllWVUEmployees(), "Id", "Fullname"); //This has to be mocked and injected into method //ViewData["Lots"] = new SelectList(iLotRepo.ListAllLots(), "LotID", "LocationName"); return(View(viewModel)); } }