public void ThrowInvalidOperationException_WithMessageContaining_OwnerCannotJoin_WhenOwnerOfATripTriesToJoinTheTrip() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); var tripId = 1; var userId = "userId"; UsersTrips IsUserOwner = new UsersTrips(); mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(IsUserOwner); Assert.That(() => tripService.RequestToJoinTrip(tripId, userId), Throws.InvalidOperationException.With.Message.Contain("owner")); }
public void ReturnFalse_WhenFoundTripIsNull() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); int tripId = 1; string userId = "driverId"; UsersTrips userTrip = null; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(userTrip); // Act var result = tripService.SignOutOfTrip(tripId, userId); // Assert mockedUserTripRepo.Verify(x => x.Delete(It.IsAny <UsersTrips>()), Times.Never); Assert.False(result); }
public void ReturnTrue_WhenUserIsTheOwner() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); string userId = "userId"; int tripId = 1; var userTrips = new UsersTrips() { TripId = tripId, UserId = userId, UserTripStatusId = (int)UserTripStatusType.Owner }; var data = new List <UsersTrips>() { userTrips }; UsersTrips exp = null; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns((Expression <Func <UsersTrips, bool> > predicate) => { exp = data.Where(predicate.Compile()).FirstOrDefault(); return(exp); }); // Act var result = tripService.IsUserOwnerOfTrip(userId, tripId); // Assert Assert.IsTrue(result); }
public void CallDeleteMethodWithFoundTrip_AndReturnTrue_WhenDeletingIsSuccesfull() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); mockedUnitOfWork.Setup(x => x.Commit()).Returns(true); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); int tripId = 1; string userId = "driverId"; Trip trip = new Trip() { Id = tripId, TakenSeats = 2 }; var userTrip = new UsersTrips() { TripId = tripId, Trip = trip, UserId = userId, UserTripStatusId = (int)UserTripStatusType.Accepted }; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(userTrip); // Act var result = tripService.SignOutOfTrip(tripId, userId); // Assert mockedUserTripRepo.Verify(x => x.Delete(userTrip), Times.Once); Assert.True(result); }
public void DecrementTakenSeats_FromTrip() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); int tripId = 1; string userId = "driverId"; Trip trip = new Trip() { Id = tripId, TakenSeats = 5 }; var userTrip = new UsersTrips() { TripId = tripId, Trip = trip, UserId = userId, UserTripStatusId = (int)UserTripStatusType.Accepted }; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(userTrip); var expectedSeats = trip.TakenSeats - 1; // Act tripService.SignOutOfTrip(tripId, userId); // Assert Assert.AreEqual(expectedSeats, trip.TakenSeats); }
public void CreateTrip(TripCreationInfo tripInfo) { var origin = this.cityService.GetCityByName(tripInfo.OriginName); if (origin == null) { origin = this.cityService.CreateCity(tripInfo.OriginName); } var destination = this.cityService.GetCityByName(tripInfo.DestinationName); if (destination == null) { destination = this.cityService.CreateCity(tripInfo.DestinationName); } var tags = this.tagService.GetTagsByIds(tripInfo.TagIds) as ICollection <Tag>; using (var uow = base.UnitOfWork()) { var trip = this.mappingProvider.Map <TripCreationInfo, Trip>(tripInfo); trip.Tags = tags; trip.Origin = origin; trip.Destination = destination; trip.DateCreated = this.dateTimeProvider.Now; var userTrips = new UsersTrips() { Trip = trip, UserId = tripInfo.DriverId, UserTripStatusId = (int)UserTripStatusType.Owner }; trip.TripsUsers = new List <UsersTrips>() { userTrips }; this.tripRepo.Add(trip); uow.Commit(); } }
public void AddNewTripUser_WithStatusOfPending_WhenUserRequestToJoin() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); var tripId = 1; var userId = "userId"; UsersTrips IsUserOwner = null; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(IsUserOwner); UsersTrips trip = null; mockedUserTripRepo.Setup(x => x.Add(It.IsAny <UsersTrips>())) .Callback((UsersTrips createdTrip) => trip = createdTrip); var expectedStatusId = (int)UserTripStatusType.Pending; // Act tripService.RequestToJoinTrip(tripId, userId); // Assert Assert.AreEqual(tripId, trip.TripId); Assert.AreEqual(userId, trip.UserId); Assert.AreEqual(expectedStatusId, trip.UserTripStatusId); }
public void Throw_InvalidOperationException_WhenTryingToDecrementBeyond0() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); int tripId = 1; string userId = "driverId"; Trip trip = new Trip() { Id = tripId, TakenSeats = 0 }; var userTrip = new UsersTrips() { TripId = tripId, Trip = trip, UserId = userId, UserTripStatusId = (int)UserTripStatusType.Accepted }; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(userTrip); // Act and Assert Assert.Throws <InvalidOperationException>(() => tripService.SignOutOfTrip(tripId, userId)); }
public void Return_False_IfUserIsOwner() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); int tripId = 1; string driverId = "driverId"; var userTrip = new UsersTrips() { TripId = tripId, UserId = driverId, UserTripStatusId = (int)UserTripStatusType.Owner }; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns(userTrip); // Act var expected = tripService.SignOutOfTrip(tripId, driverId); // Assert Assert.False(expected); }
public void MarkTripAsFinished_WhenParamsAreValid() { // Arrange var mockedTripRepo = new Mock <IProjectableRepositoryEf <Trip> >(); var mockedUserTripRepo = new Mock <IProjectableRepositoryEf <UsersTrips> >(); var mockedCityService = new Mock <ICityService>(); var mockedTagService = new Mock <ITagService>(); var mockedDateTimpeProvider = new Mock <IDateTimeProvider>(); var mockedMappingProvider = new Mock <IMappingProvider>(); var mockedUnitOfWork = new Mock <IUnitOfWorkEF>(); mockedUnitOfWork.Setup(x => x.Commit()).Returns(true); var tripService = new TripService( () => mockedUnitOfWork.Object, mockedUserTripRepo.Object, mockedCityService.Object, mockedMappingProvider.Object, mockedTagService.Object, mockedTripRepo.Object, mockedDateTimpeProvider.Object); string userId = "userId"; int tripId = 1; var trip = new Trip() { Id = tripId }; var userTrips = new UsersTrips() { TripId = tripId, UserId = userId, UserTripStatusId = (int)UserTripStatusType.Owner }; var data = new List <UsersTrips>() { userTrips }; UsersTrips exp = null; mockedUserTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <UsersTrips, bool> > >())) .Returns((Expression <Func <UsersTrips, bool> > predicate) => { exp = data.Where(predicate.Compile()).FirstOrDefault(); return(exp); }); mockedTripRepo.Setup(x => x.GetFirst(It.IsAny <Expression <Func <Trip, bool> > >())) .Returns(trip); // Act var result = tripService.MarkTripAsFinished(tripId, userId); // Assert Assert.IsTrue(trip.IsFinished); mockedUnitOfWork.Verify(x => x.Commit(), Times.Once); Assert.IsTrue(result); }