public async Task ViewShift_NoShifts_ViewDataWarning() { // Arrange DateTime now = DateTime.Now; ILocationService locationService = Substitute.For <ILocationService>(); locationService.GetByName("Contoso").Returns(new Library.Models.Location { id = "1", Name = "Contoso" }); IStringLocalizer <ShiftsController> stringLocalizer = Substitute.For <IStringLocalizer <ShiftsController> >(); stringLocalizer.GetString("NoEmployees").Returns(new LocalizedString("NoEmployees", "No employees are booked for this shift.")); ShiftsController controller = this.GetController(locationService: locationService, stringLocalizer: stringLocalizer); ShiftViewModel viewModel = new ShiftViewModel { LocationName = "Contoso", Start = now, End = now.AddHours(9), WorkType = "Till" }; // Act IActionResult result = await controller.ViewShift(viewModel); // Assert ViewResult viewResult = Assert.IsType <ViewResult>(result); Assert.Equal(stringLocalizer["NoEmployees"], viewResult.ViewData["NoEmployees"]); }
public async Task ViewShift_EmployeesOnShift_ShiftRepoHitWithCorrectQuery() { // Arrange IRepository <Library.Models.Shift> shiftRepository = Substitute.For <IRepository <Library.Models.Shift> >(); DateTime now = DateTime.Now; shiftRepository.Get(Arg.Any <string>(), Arg.Any <IDictionary <string, object> >(), Arg.Any <string>()).Returns(new[] { new Library.Models.Shift { StartDateTime = now, LocationId = "1", Allocated = true, EmployeeId = "abc" }, new Library.Models.Shift { StartDateTime = now.Add(new TimeSpan(1, 0, 0, 0)), LocationId = "1", Allocated = true, EmployeeId = "xyz" } }); ILocationService locationService = Substitute.For <ILocationService>(); locationService.GetByName("Contoso").Returns(new Library.Models.Location { id = "1", Name = "Contoso" }); IGraphServiceClient graphClient = Substitute.For <IGraphServiceClient>(); graphClient.Users[Arg.Any <string>()].Request().GetAsync().Returns(new User { GivenName = "a", Surname = "b" }); ShiftsController controller = this.GetController(shiftRepository: shiftRepository, locationService: locationService, graphClient: graphClient); ShiftViewModel viewModel = new ShiftViewModel { LocationName = "Contoso", Start = now, End = now.AddHours(9), WorkType = "Till" }; // Act await controller.ViewShift(viewModel); // Assert #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed shiftRepository.Received(1).Get( "SELECT * FROM c WHERE c.LocationId = @locationId AND c.StartDateTime = @start AND c.EndDateTime = @end AND c.WorkType = @workType", Arg.Any <Dictionary <string, object> >(), Arg.Any <string>()); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed }
public async Task ViewShift_EmployeesOnShift_EmployeesInViewData() { // Arrange IRepository <Library.Models.Shift> shiftRepository = Substitute.For <IRepository <Library.Models.Shift> >(); DateTime now = DateTime.Now; shiftRepository.Get(Arg.Any <string>(), Arg.Any <IDictionary <string, object> >(), Arg.Any <string>()).Returns(new[] { new Library.Models.Shift { StartDateTime = now, LocationId = "1", Allocated = true, EmployeeId = "abc" }, new Library.Models.Shift { StartDateTime = now.Add(new TimeSpan(1, 0, 0, 0)), LocationId = "1", Allocated = true, EmployeeId = "xyz" } }); ILocationService locationService = Substitute.For <ILocationService>(); locationService.GetByName("Contoso").Returns(new Library.Models.Location { id = "1", Name = "Contoso" }); IGraphServiceClient graphClient = Substitute.For <IGraphServiceClient>(); graphClient.Users[Arg.Any <string>()].Request().GetAsync().Returns(new User { GivenName = "a", Surname = "b" }); ShiftsController controller = this.GetController(shiftRepository: shiftRepository, locationService: locationService, graphClient: graphClient); ShiftViewModel viewModel = new ShiftViewModel { LocationName = "Contoso", Start = now, End = now.AddHours(9), WorkType = "Till" }; // Act IActionResult result = await controller.ViewShift(viewModel); // Assert ViewResult viewResult = Assert.IsType <ViewResult>(result); List <string> employees = Assert.IsType <List <string> >(viewResult.ViewData["Employees"]); Assert.Equal(2, employees.Count()); }