public async Task Search_SearchViewModel_ResultsFilteredByLocation() { // Arrange IRepository <Library.Models.Shift> shiftRepository = Substitute.For <IRepository <Library.Models.Shift> >(); shiftRepository.Get(Arg.Any <string>(), Arg.Any <IDictionary <string, object> >(), Arg.Any <string>()).Returns(new [] { new Library.Models.Shift { StartDateTime = DateTime.Now.Add(new TimeSpan(1, 0, 0, 0)), LocationId = "1" }, }); ILocationService locationService = Substitute.For <ILocationService>(); locationService.Get().Returns(new[] { new Library.Models.Location { id = "1", Name = "Contoso" }, new Library.Models.Location { id = "2", Name = "Fabrikam" } }); ShiftsController controller = this.GetController(shiftRepository: shiftRepository, locationService: locationService); SearchShiftViewModel viewModel = new SearchShiftViewModel { Locations = new List <string> { "Contoso" }, Start = DateTime.Now }; // Act IActionResult result = await controller.Index(viewModel); // Assert ViewResult viewResult = Assert.IsType <ViewResult>(result); SearchShiftViewModel viewModelResult = Assert.IsAssignableFrom <SearchShiftViewModel>(viewResult.ViewData.Model); Assert.Contains <ShiftViewModel>(viewModelResult.Result, x => x.LocationName == "Contoso"); Assert.DoesNotContain <ShiftViewModel>(viewModelResult.Result, x => x.LocationName == "Fabrikam"); }
public async Task Index_NoParams_RedirectIfNoLocations() { // Arrange ShiftsController controller = this.GetController(); controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("extension_zaprole", "org_a_manager") })); // Act IActionResult result = await controller.Index(); // Assert RedirectResult redirectResult = Assert.IsType <RedirectResult>(result); Assert.Equal("/Locations", redirectResult.Url); }
public async Task Index_NoParams_ReturnsShiftsForNowAndLater() { // Arrange IRepository <Library.Models.Shift> shiftRepository = Substitute.For <IRepository <Library.Models.Shift> >(); ILocationService locationService = Substitute.For <ILocationService>(); locationService.Get().Returns(new[] { new Library.Models.Location { id = "1", Name = "Contoso" } }); ShiftsController controller = this.GetController(shiftRepository: shiftRepository, locationService: locationService); // Act await controller.Index(); // 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.StartDateTime >= @startDateTime", Arg.Is <Dictionary <string, object> >(x => x.ContainsKey("@startDateTime"))); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed }
public async Task Search_SearchViewModel_LocationsServiceHitOnce() { // Arrange ILocationService locationService = Substitute.For <ILocationService>(); ShiftsController controller = this.GetController(locationService: locationService); controller.HttpContext.User = new ClaimsPrincipal(new ClaimsIdentity(new[] { new Claim("extension_zaprole", "org_a_manager") })); SearchShiftViewModel viewModel = new SearchShiftViewModel { Locations = new List <string> { "Contoso" }, Start = DateTime.Now }; // Act await controller.Index(viewModel); // Assert #pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed locationService.Received(1).Get(); #pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed }