public async void Create() { var tripServiceMock = new Mock <ITripService> (); var pathServiceMock = new Mock <IPathService> (); string lineID = "Line:1"; string pathID = "Path:1"; string tripDepartureTime = "20:12:10"; var trip = new Trip(lineID, pathID, tripDepartureTime); var tripDTO = new TripDTO(trip.Id.AsGuid(), new LineId(lineID), new PathId(pathID), tripDepartureTime); var path = new PathId(pathID); var pathDTO = new PathDTO(pathID, true, new List <SegmentDTO> ()); var creatingTrip = new CreatingTripDTO(lineID, pathID, tripDepartureTime); pathServiceMock.Setup(_ => _.GetById(path)).ReturnsAsync(pathDTO); tripServiceMock.Setup(_ => _.AddTrip(creatingTrip, new List <CreatingNodePassageDTO> ())).ReturnsAsync(tripDTO); var controller = new TripController(tripServiceMock.Object, pathServiceMock.Object); var actual = await controller.Create(creatingTrip); Assert.NotNull(actual); Assert.NotNull(actual.Result); }
public void SetModelStateToErrorWithCorrectMessageWhenTripIsSetupInThePast() { //Arrange this.tripsServiceMock.Setup(s => s.GetTripByName(It.IsAny <string>())) .Returns((Trip)null); var controller = new TripController( this.usersServiceMock.Object, this.tripsServiceMock.Object, this.cacheServiceMock.Object, this.mapperMock.Object); var viewModelMock = new Mock <CreateTripViewModel>().Object; viewModelMock.LocalTimeOffsetMinutes = 100; viewModelMock.TripDate = new DateTime(2010, 10, 10, 10, 10, 10); var expected = ErrorMessageConstants.TripDateInThePast; //Act controller.Create(viewModelMock); //Assert var errorMessage = controller.ModelState["TripDate"].Errors[0].ErrorMessage; Assert.AreEqual(expected, errorMessage); }
public void Create_should_return_view() { TripController controller = new TripController(repository, provider); ViewResult result = controller.Create() as ViewResult; Assert.IsNotNull(result); }
public void SetModelStateToErrorWithCorrectMessageWhenTripNameAlreadyExistsInComingTrips() { //Arrange var tripMock = new Mock <Trip>().Object; this.tripsServiceMock.Setup(s => s.GetTripByName(It.IsAny <string>())) .Returns(tripMock); var controller = new TripController( this.usersServiceMock.Object, this.tripsServiceMock.Object, this.cacheServiceMock.Object, this.mapperMock.Object); var viewModelMock = new Mock <CreateTripViewModel>().Object; var expected = ErrorMessageConstants.TripNameAlreadyExists; //Act controller.Create(viewModelMock); //Assert var errorMessage = controller.ModelState["TripName"].Errors[0].ErrorMessage; Assert.AreEqual(expected, errorMessage); }
public void CallCaceServiceRemoveMethodOnce() { //Arrange var cacheServiceMock = new Mock <ICacheService>(); tripsServiceMock.Setup(s => s.GetTripByName(It.IsAny <string>())) .Returns((Trip)null); var controller = new TripController( this.usersServiceMock.Object, this.tripsServiceMock.Object, cacheServiceMock.Object, this.mapperMock.Object); var viewModelMock = new Mock <CreateTripViewModel>().Object; viewModelMock.LocalTimeOffsetMinutes = 100; viewModelMock.TripDate = new DateTime(2020, 10, 10, 10, 10, 10); var tripMock = new Mock <Trip>().Object; mapperMock.Setup(m => m.Map <Trip>(viewModelMock)).Returns(tripMock); var fullTripViewModelMock = new Mock <FullTripViewModel>(); mapperMock.Setup(m => m.Map <FullTripViewModel>(It.IsAny <Trip>())).Returns(fullTripViewModelMock.Object); //Act controller.Create(viewModelMock); //Assert cacheServiceMock.Verify(c => c.Remove(It.IsAny <string>()), Times.Once); }
public async Task CannotCreateTripWithSameTitleForSameUser() { TestTrekStoriesContext tc = new TestTrekStoriesContext(); Trip trip = new Trip { Title = "Test", Country = "Ireland", TripCategory = TripCategory.forest, StartDate = new DateTime(2015, 4, 12), TripOwner = "User1" }; tc.Trips.Add(trip); Trip newTrip = new Trip { Title = "Test", Country = "Spain", TripCategory = TripCategory.coast, StartDate = new DateTime(2015, 4, 12), TripOwner = "User1" }; var controller = new TripController(tc).WithAuthenticatedUser("User1"); var result = await controller.Create(newTrip) as ViewResult; Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.IsTrue(controller.ViewData.ModelState.Count == 1, "You have already created a trip with that title. Please give this trip a different title."); }
public void Create_trip_adds_to_repository() { TripController controller = new TripController(repository, provider); Trip newTrip = new Trip(provider.AuthenticatedUser.UserID); newTrip.current_weight = 8; newTrip.description = "London Trip"; newTrip.destination = "London"; newTrip.total_capacity = 12; newTrip.weather = "Cloudy"; controller.Create(newTrip); ViewResult result = controller.Index() as ViewResult; IQueryable <Trip> model = result.Model as IQueryable <Trip>; Assert.IsTrue(model.Contains(newTrip)); Assert.AreEqual(2, model.Count()); }
public void SetModelDescriptionIfNull() { //Arrange var controller = new TripController( this.usersServiceMock.Object, this.tripsServiceMock.Object, this.cacheServiceMock.Object, this.mapperMock.Object); var viewModelMock = new Mock <CreateTripViewModel>().Object; //Act controller.Create(viewModelMock); // Assert.AreEqual(viewModelMock.Description, UiMessageConstants.NoTripDescription); }
public async Task CanCreateTrip() { TestTrekStoriesContext tc = new TestTrekStoriesContext(); Trip newTrip = new Trip { Title = "Test Trip", Country = "Ireland", TripCategory = TripCategory.forest, StartDate = new DateTime(2015, 4, 12) }; var controller = new TripController(tc).WithAuthenticatedUser("ABC123"); var result = await controller.Create(newTrip) as RedirectToRouteResult; string owner = tc.Trips.Find(newTrip.TripId).TripOwner; Assert.AreEqual("Details", result.RouteValues["action"]); Assert.AreEqual("ABC123", owner); }
public async Task CannotCreateTripWithModelErrors() { TestTrekStoriesContext tc = new TestTrekStoriesContext(); Trip newTrip = new Trip { Title = "", Country = "Ireland", TripCategory = TripCategory.forest, StartDate = new DateTime(2015, 4, 12), TripOwner = "ABC123" }; var controller = new TripController(tc); controller.ModelState.AddModelError("", "Error"); var result = await controller.Create(newTrip) as ViewResult; Assert.AreEqual("", result.ViewName); Assert.AreEqual(false, result.ViewData.ModelState.IsValid); Assert.IsNotNull(result.ViewData.ModelState[""].Errors); }
public void SetModelStateToErrorWithCorrectTypeWhenTripNameAlreadyExistsInComingTrips() { //Arrange var tripMock = new Mock <Trip>().Object; this.tripsServiceMock.Setup(s => s.GetTripByName(It.IsAny <string>())) .Returns(tripMock); var controller = new TripController( this.usersServiceMock.Object, this.tripsServiceMock.Object, this.cacheServiceMock.Object, this.mapperMock.Object); var viewModelMock = new Mock <CreateTripViewModel>().Object; //Act controller.Create(viewModelMock); //Assert Assert.IsTrue(controller.ModelState.Values.Contains(controller.ModelState["TripName"])); }
public void SetModelStateToErrorWithCorrectTypeWhenTripIsSetupInThePast() { //Arrange this.tripsServiceMock.Setup(s => s.GetTripByName(It.IsAny <string>())) .Returns((Trip)null); var controller = new TripController( this.usersServiceMock.Object, this.tripsServiceMock.Object, this.cacheServiceMock.Object, this.mapperMock.Object); var viewModelMock = new Mock <CreateTripViewModel>().Object; viewModelMock.LocalTimeOffsetMinutes = 100; viewModelMock.TripDate = new DateTime(2010, 10, 10, 10, 10, 10); //Act controller.Create(viewModelMock); //Assert Assert.IsTrue(controller.ModelState.Values.Contains(controller.ModelState["TripDate"])); }