public void WhenJsonList_ThenReturnsVehicles() { var vehicles = new[] { new VehicleModel(new Vehicle { Name = "test" }, new VehicleStatisticsModel()) }; MockHandlerFor( () => new Mock <GetVehicleListForUser>(null), mock => mock .Setup(h => h.Execute(It.IsAny <int>())) .Returns(vehicles) ); TestableVehicleController controller = GetTestableVehicleController(); JsonResult result = controller.JsonList(); Assert.IsType <JsonResult>(result); var data = (IList <JsonVehicleViewModel>)result.Data; Assert.NotNull(data); Assert.Equal(vehicles.First().Name, data.First().Name); }
public void WhenRequestingVehicleDetails_ThenSetsRemindersList() { const int selectedVehicleId = 99; const int odometer = 15000; var vehicle = new VehicleModel( new Vehicle { VehicleId = selectedVehicleId }, new VehicleStatisticsModel(0, 0, 0, 0, odometer, 0) ); var reminder = new ReminderSummaryModel(new Reminder(), false); MockHandlerFor( () => new Mock <GetVehicleListForUser>(null), m => m.Setup(h => h.Execute(defaultUser.UserId)) .Returns(new[] { vehicle })); Mock <GetOverdueRemindersForVehicle> handler = MockHandlerFor( () => new Mock <GetOverdueRemindersForVehicle>(null), m => m.Setup(h => h.Execute(selectedVehicleId, It.IsAny <DateTime>(), odometer)) .Returns(new[] { reminder }) .Verifiable("Did not get overdue reminders.") ); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Details(selectedVehicleId); var model = result.Extract <VehicleDetailsViewModel>(); handler.Verify(); Assert.NotNull(model.OverdueReminders); Assert.Same(reminder, model.OverdueReminders.First()); }
public void WhenRequestingDashboard_ThenInvokeHandlers() { var list = MockHandlerFor( () => new Mock <GetVehicleListForUser>(null), mock => mock .Setup(h => h.Execute(defaultUser.UserId)) .Returns(new VehicleModel[] {}) .Verifiable("handler wasn't invoked.") ); var reminders = MockHandlerFor( () => new Mock <GetImminentRemindersForUser>(null, null), mock => mock .Setup(h => h.Execute(defaultUser.UserId, It.IsAny <DateTime>(), NoVehicleSelectedId)) .Verifiable("handler wasn't invoked.") ); var statistics = MockHandlerFor( () => new Mock <GetFleetSummaryStatistics>(null), mock => mock .Setup(h => h.Execute(defaultUser.UserId)) .Verifiable("handler wasn't invoked.") ); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.List(); Assert.IsType <ViewResult>(result); list.Verify(); reminders.Verify(); statistics.Verify(); }
public void WhenEditGetFormActionExecuted_ThenSelectListsPopulated() { var data = new Tuple <int[], string[], string[]>( new[] { 1, 2, 3, 4, 5 }, new[] { "A", "B" }, new[] { "i", "ii", "iii" } ); MockHandlerFor( () => new Mock <GetYearsMakesAndModels>(null), x => x .Setup(h => h.Execute(null, null)) .Returns(data)); MockVehicleListWithVehicles(); TestableVehicleController controller = GetTestableVehicleController(); var actual = (ViewResult)controller.Edit(DefaultVehicleId); var yearsSelectList = (SelectList)actual.ViewData["Years"]; Assert.NotNull(yearsSelectList); Assert.Equal(5, yearsSelectList.Count()); var makesSelectList = (SelectList)actual.ViewData["Makes"]; Assert.NotNull(makesSelectList); Assert.Equal(2, makesSelectList.Count()); var modelsSelectList = (SelectList)actual.ViewData["Models"]; Assert.NotNull(modelsSelectList); Assert.Equal(3, modelsSelectList.Count()); }
public void WhenPopulatingSelectList_ThenItemsHaveTextAndValueProperties() { var data = new Tuple <int[], string[], string[]>( new[] { 2011 }, new[] { "Manufacturer" }, new[] { "ModelA", "ModelB", "ModelC" } ); MockHandlerFor( () => new Mock <GetYearsMakesAndModels>(null), x => x .Setup(h => h.Execute(null, null)) .Returns(data)); MockVehicleListWithVehicles(); TestableVehicleController controller = GetTestableVehicleController(); var actual = (ViewResult)controller.Edit(DefaultVehicleId); var yearsSelectList = (SelectList)actual.ViewData["Years"]; Assert.Equal("2011", yearsSelectList.First().Text); Assert.Equal("2011", yearsSelectList.First().Value); var makesSelectList = (SelectList)actual.ViewData["Makes"]; Assert.Equal("Manufacturer", makesSelectList.First().Text); Assert.Equal("Manufacturer", makesSelectList.First().Value); var modelsSelectList = (SelectList)actual.ViewData["Models"]; Assert.Equal("ModelB", modelsSelectList.Skip(1).First().Text); Assert.Equal("ModelB", modelsSelectList.Skip(1).First().Value); }
public void WhenAddActionExecuted_ThenVehicleListSetToCollapsed() { MockDefaultYearMakeModel(); MockVehicleList(); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Add(); var model = result.Extract <VehicleAddViewModel>(); Assert.True(model.VehiclesList.IsCollapsed); }
public void WhenRequestingDashboard_ThenCountryListIsAddedToViewBag() { MockHandlers(); TestableVehicleController controller = GetTestableVehicleController(); ActionResult response = controller.List(); var result = (ViewResult)response; Assert.NotNull(result.ViewBag.CountryList); }
public void WhenAddActionExecuted_ThenViewModelContainsVehicleListViewModel() { MockVehicleList(); MockDefaultYearMakeModel(); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Add(); var model = result.Extract <VehicleAddViewModel>(); Assert.NotNull(model); }
public void WhenAddActionExecuted_ThenResultPopulated() { MockVehicleList(); MockDefaultYearMakeModel(); TestableVehicleController controller = GetTestableVehicleController(); var actual = controller.Add() as ViewResult; Assert.NotNull(actual); Assert.Equal(string.Empty, actual.ViewName); }
public void WhenRequestingDashboard_ThenReturnsUser() { MockHandlers(); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.List(); var model = result.Extract <DashboardViewModel>(); Assert.NotNull(model); Assert.NotNull(model.User); Assert.Same(defaultUser, model.User); }
public void WhenEditGetFormActionExecuted_ThenViewModelContainsVehicleListViewModel() { MockVehicleListWithVehicles(); MockDefaultYearMakeModel(); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Edit(DefaultVehicleId); var model = result.Extract <VehicleAddViewModel>(); Assert.IsType <VehicleAddViewModel>(model); }
public void WhenJsonDeleteIsCalled_ThenDeletesVehicle() { Mock <DeleteVehicle> handler = MockHandlerFor( () => new Mock <DeleteVehicle>(null), x => x.Setup(h => h.Execute(defaultUser.UserId, DefaultVehicleId)) .Verifiable()); TestableVehicleController controller = GetTestableVehicleController(); controller.JsonDelete(DefaultVehicleId); handler.Verify(); }
// returns controller with mocks private TestableVehicleController GetTestableVehicleController() { var c = new TestableVehicleController(userServicesMock.Object, serviceLocator.Object); c.SetFakeControllerContext(); c.SetUserIdentity(new MileageStatsIdentity(defaultUserInfo.ClaimsIdentifier, defaultUserInfo.DisplayName, defaultUserInfo.UserId)); c.InvokeInitialize(c.ControllerContext.RequestContext); return(c); }
public void WhenAddVehicleActionExecutedWithInvalidVehicle_ViewModelContainsVehicleFormViewModel() { MockVehicleList(); MockDefaultYearMakeModel(); TestableVehicleController controller = GetTestableVehicleController(); controller.ModelState.AddModelError("bad", "bad"); ActionResult result = controller.Add(new VehicleFormModel(), null, "Save"); var model = result.Extract <VehicleFormModel>(); Assert.NotNull(model); }
public void WhenVehicleDeleted_ThenCallsServicesDelete() { const int vehicleToDelete = 99; var handler = MockHandlerFor <DeleteVehicle>( mock => mock.Setup(h => h.Execute(defaultUser.UserId, vehicleToDelete)) .Verifiable("delete handler was not invoke") ); TestableVehicleController controller = GetTestableVehicleController(); controller.Delete(vehicleToDelete); handler.Verify(); }
public void WhenVehicleDeleted_ThenRedirectsToDashboard() { MockHandlerFor( () => new Mock <DeleteVehicle>(null), mock => mock.Setup(h => h.Execute(defaultUser.UserId, DefaultVehicleId)) ); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Delete(DefaultVehicleId); Assert.IsType <RedirectToRouteResult>(result); var redirect = (RedirectToRouteResult)result; Assert.Equal("Dashboard", redirect.RouteName); }
public void WhenUpdatingVehicleSortOrder_InvokesHandler() { Mock <UpdateVehicleSortOrder> handler = MockHandlerFor(() => new Mock <UpdateVehicleSortOrder>(null)); const string newOrder = "3,2,1"; var sortOrder = new UpdateVehicleSortOrderViewModel { SortOrder = newOrder }; TestableVehicleController controller = GetTestableVehicleController(); controller.UpdateSortOrder(sortOrder); handler.Verify(x => x.Execute(defaultUserInfo.UserId, new[] { 3, 2, 1 })); }
public void WhenRequestingVehicleDetails_ThenSetsVehicleListToCollapsedView() { MockHandlerFor( () => new Mock <GetVehicleListForUser>(null), x => x .Setup(h => h.Execute(defaultUser.UserId)) .Returns(DefaultVehicleId.StandardVehicleList())); MockHandlerFor(() => new Mock <GetOverdueRemindersForVehicle>(null)); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Details(DefaultVehicleId); var model = result.Extract <VehicleDetailsViewModel>(); Assert.True(model.VehicleList.IsCollapsed); }
public void WhenAddVehicleActionExecutedWithInvalidVehicle_ThenVehicleListSetToCollapsed() { MockVehicleList(); MockDefaultYearMakeModel(); TestableVehicleController controller = GetTestableVehicleController(); controller.ModelState.AddModelError("bad", "bad"); ActionResult result = controller.Add(new FormCollection { { "Save", "true" } }, new VehicleFormModel(), null); var model = result.Extract <VehicleAddViewModel>(); Assert.True(model.VehiclesList.IsCollapsed); }
public void WhenRequestingVehicleDetailsWithoutAValidVehicle_ThenThrows() { var list = new VehicleModel[] { }; MockHandlerFor( () => new Mock <GetVehicleListForUser>(null), x => x .Setup(h => h.Execute(defaultUser.UserId)) .Returns(list)); MockHandlerFor(() => new Mock <GetOverdueRemindersForVehicle>(null)); TestableVehicleController controller = GetTestableVehicleController(); Assert.Throws <InvalidOperationException>(() => { controller.Details(DefaultVehicleId); }); }
public void WhenEditVehicleActionExecutedWithInvalidVehicle_ThenSelectListsPopulated() { const int year = 1984; const string make = "make"; var data = new Tuple <int[], string[], string[]>( new[] { 1, 2, 3, 4, 5 }, new[] { "A", "B" }, new[] { "i", "ii", "iii" } ); MockHandlerFor( () => new Mock <GetYearsMakesAndModels>(null), x => x .Setup(h => h.Execute(year, make)) .Returns(data)); MockVehicleListWithVehicles(); TestableVehicleController controller = GetTestableVehicleController(); controller.ModelState.AddModelError("bad", "bad"); var vehicleForm = new VehicleFormModel { VehicleId = DefaultVehicleId, Name = null, Year = year, MakeName = make }; var form = new FormCollection { { "Save", "true" } }; var actual = (ViewResult)controller.Edit(form, vehicleForm, null); var yearsSelectList = (SelectList)actual.ViewData["Years"]; Assert.NotNull(yearsSelectList); Assert.Equal(5, yearsSelectList.Count()); var makesSelectList = (SelectList)actual.ViewData["Makes"]; Assert.NotNull(makesSelectList); Assert.Equal(2, makesSelectList.Count()); var modelsSelectList = (SelectList)actual.ViewData["Models"]; Assert.NotNull(modelsSelectList); Assert.Equal(3, modelsSelectList.Count()); }
public void WhenEditGetFormActionExecuted_ThenVehicleListSetToCollapsed() { MockDefaultYearMakeModel(); MockHandlerFor( () => new Mock <GetVehicleListForUser>(null), x => x .Setup(h => h.Execute(defaultUser.UserId)) .Returns(DefaultVehicleId.StandardVehicleList())); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Edit(DefaultVehicleId); var model = result.Extract <VehicleAddViewModel>(); Assert.True(model.VehiclesList.IsCollapsed); }
public void WhenJsonFleetStats_ThenReturnsFleetStatistics() { var fleetSummaryStatistics = new FleetStatistics(new VehicleStatisticsModel[0]); MockHandlerFor( () => new Mock <GetFleetSummaryStatistics>(null), x => x.Setup(h => h.Execute(defaultUser.UserId)) .Returns(fleetSummaryStatistics) ); TestableVehicleController controller = GetTestableVehicleController(); JsonResult result = controller.JsonFleetStatistics(); Assert.NotNull(result); Assert.Same(fleetSummaryStatistics, result.Data); }
public void WhenJsonMakesRequestedForDatalessYear_ThenReturnsNoMakes() { const int yearSelected = 1985; MockHandlerFor( () => new Mock <GetYearsMakesAndModels>(null), x => x .Setup(h => h.Execute(yearSelected, null)) .Returns(YearsMakesAndModelsFor.YearWithoutMakes(yearSelected))); TestableVehicleController controller = GetTestableVehicleController(); JsonResult result = controller.MakesForYear(yearSelected); var makeList = (string[])result.Data; Assert.Equal(0, makeList.Count()); }
public void WhenPhotoForValidPhoto_ThenPhotoReturned() { const int photoId = 33; MockHandlerFor( () => new Mock <GetVehiclePhoto>(null), x => x .Setup(h => h.Execute(photoId)) .Returns(new VehiclePhoto { Image = new byte[] { }, ImageMimeType = "something" })); TestableVehicleController controller = GetTestableVehicleController(); var result = (FileStreamResult)controller.Photo(photoId); Assert.NotNull(result.FileStream); }
public void WhenJsonModelsRequestedForDatalessYearAndMake_ThenReturnsModels() { const int yearSelected = 1985; const string makeSelected = "ManufacturerA"; MockHandlerFor( () => new Mock <GetYearsMakesAndModels>(null), x => x .Setup(h => h.Execute(yearSelected, makeSelected)) .Returns(YearsMakesAndModelsFor.MakeWithModels(yearSelected, makeSelected))); TestableVehicleController controller = GetTestableVehicleController(); JsonResult result = controller.ModelsForMake(yearSelected, makeSelected); var modelList = (string[])result.Data; Assert.Equal(0, modelList.Count()); }
public void WhenJsonDetailsCalled_ThenReturnsVehicle() { var vehicle = new Vehicle { VehicleId = DefaultVehicleId, Name = "test", MakeName = "make", ModelName = "model", Year = 2010, PhotoId = 12, SortOrder = 1, Fillups = new List <FillupEntry>() }; MockHandlerFor( () => new Mock <GetVehicleById>(null), x => x .Setup(h => h.Execute(It.IsAny <int>(), DefaultVehicleId)) .Returns(new VehicleModel(vehicle, new VehicleStatisticsModel()))); MockHandlerFor( () => new Mock <GetOverdueRemindersForVehicle>(null), x => x .Setup(h => h.Execute(DefaultVehicleId, It.IsAny <DateTime>(), 0)) .Returns(new List <ReminderSummaryModel> { new ReminderSummaryModel(new Reminder(), isOvedue: true) })); TestableVehicleController controller = GetTestableVehicleController(); JsonResult result = controller.JsonDetails(DefaultVehicleId); var data = (JsonVehicleViewModel)result.Data; Assert.Equal(DefaultVehicleId, data.VehicleId); Assert.Equal("test", data.Name); Assert.Equal("make", data.MakeName); Assert.Equal("model", data.ModelName); Assert.Equal(2010, data.Year); Assert.Equal(12, data.PhotoId); Assert.Equal(1, data.SortOrder); Assert.NotEmpty(data.OverdueReminders); }
public void WhenRequestingVehicleDetails_ThenSetsViewModel() { var vehicle = new Vehicle { VehicleId = DefaultVehicleId }; var list = new[] { new VehicleModel(vehicle, new VehicleStatisticsModel()) }; MockHandlerFor <GetVehicleListForUser>( x => x.Setup(h => h.Execute(defaultUser.UserId)) .Returns(list)); MockHandlerFor <GetOverdueRemindersForVehicle>(); TestableVehicleController controller = GetTestableVehicleController(); ActionResult result = controller.Details(DefaultVehicleId); var model = result.Extract <VehicleDetailsViewModel>(); Assert.NotNull(model); }
public void WhenEditVehicleActionExecutedWithInValidVehicle_ThenVehicleListSetToCollapsed() { MockVehicleListWithVehicles(); MockDefaultYearMakeModel(); TestableVehicleController controller = GetTestableVehicleController(); controller.ModelState.AddModelError("bad", "bad"); var vehicle = new VehicleFormModel { VehicleId = 1, Name = null }; var form = new FormCollection { { "Save", "true" } }; ActionResult result = controller.Edit(form, vehicle, null); var model = result.Extract <VehicleAddViewModel>(); Assert.True(model.VehiclesList.IsCollapsed); }
public void WhenAddVehicleActionExecutedWithValidVehicle_ThenVehicleIsCreated() { var vehicleForm = new VehicleFormModel(); MockVehicleList(); MockDefaultYearMakeModel(); MockHandlerFor(() => new Mock <CanAddVehicle>(null, null)); var handler = MockHandlerFor( () => new Mock <CreateVehicle>(null, null), x => x .Setup(h => h.Execute(defaultUser.UserId, vehicleForm, null)) .Verifiable("handler not invoked")); TestableVehicleController controller = GetTestableVehicleController(); controller.Add(new FormCollection { { "Save", "true" } }, vehicleForm, null); handler.Verify(); }