Ejemplo n.º 1
0
        public async Task AddRideAsync_WhenRideIsASoloRide_ReturnsACreateRideResponseContainingTheCorrectPrice()
        {
            decimal distance        = 10;
            decimal calculatedPrice = 100;

            _googleMapsApiService.GetDistanceInKmAsync(Arg.Any <string>(), Arg.Any <string>())
            .ReturnsForAnyArgs(distance);
            _soloRidePriceStrategy.CalculatePrice(distance).Returns(calculatedPrice); //Stub the solo strategy
            var expectedResponse = new CreateRideResponse {
                Price = calculatedPrice
            };

            _mapper.Map <CreateRideResponse>(null).ReturnsForAnyArgs(expectedResponse);
            _mapper.Map <SoloRide>(null)
            .ReturnsForAnyArgs(new SoloRide {
                StartDestination = _anAddress, EndDestination = _anAddress
            });
            var request = new CreateRideRequest
            {
                StartDestination = _anAddress,
                EndDestination   = _anAddress,
                RideType         = RideType.SoloRide
            };

            var response = await _rideService.AddRideAsync(request, "aCustomerId");

            Assert.That(response.Price, Is.EqualTo(calculatedPrice));
        }
Ejemplo n.º 2
0
        public async Task AddRideAsync_WhenRideIsASharedRide_ReturnsACreateRideResponseWithWaitingForAccept()
        {
            decimal distance        = 10;
            decimal calculatedPrice = 100;

            _googleMapsApiService.GetDistanceInKmAsync(Arg.Any <string>(), Arg.Any <string>())
            .ReturnsForAnyArgs(distance);
            _sharedRidePriceStrategy.CalculatePrice(distance).Returns(calculatedPrice);
            var expectedResponse = new CreateRideResponse {
                Price = calculatedPrice
            };

            _mapper.Map <CreateRideResponse>(null).ReturnsForAnyArgs(expectedResponse);
            _mapper.Map <SharedRide>(null)
            .ReturnsForAnyArgs(new SharedRide {
                StartDestination = _anAddress, EndDestination = _anAddress
            });
            var request = new CreateRideRequest
            {
                StartDestination = _anAddress,
                EndDestination   = _anAddress,
                RideType         = RideType.SharedRide
            };

            //Must have ID because otherwise both are null
            _unitOfWork.RideRepository.FindUnmatchedSharedRides().ReturnsForAnyArgs(new List <Ride>()
            {
                new Ride()
                {
                    Id = 3,
                    StartDestination = _anAddress,
                    EndDestination   = _anAddress,
                }
            });
            _matchService.Match(null, null, 0).ReturnsForAnyArgs(true);

            var response = await _rideService.AddRideAsync(request, "aCustomerId");

            //Validate through savechanges were called twice,
            //To acknowledge that the rides have matched.
            _unitOfWork.Received(2).SaveChangesAsync();
        }
Ejemplo n.º 3
0
        private async void CreateRideCommandExecuteAsync()
        {
            IsBusy = true;
            CreateRideResponse response = await _backendApiService.SubmitCreateRideRequest(Request);

            IsBusy = false;

            //Debug.WriteLine(response.HttpResponseMessage.StatusCode);

            if (response == null)
            {
                await DialogService.DisplayAlertAsync("Forbindelse", "Du har ikke forbindelse til internettet", "OK");
            }
            else if (response.WasUnsuccessfull())
            {
                await DialogService.DisplayAlertAsync("Fejl", response.Body.errors.First().Value[0], "OK");
            }
            else if (response.WasSuccessfull())
            {
                await DialogService.DisplayAlertAsync("Success", "Turen er blevet oprettet \nAt betale: " + response.Body.price + " kr.", "OK");

                await NavigationService.GoBackAsync();
            }
        }
        public void SetUp()
        {
            _fakeBackendApiService = Substitute.For <IBackendApiService>();
            _fakeNavigationService = Substitute.For <INavigationService>();
            _fakePageDialogService = Substitute.For <IPageDialogService>();
            _uut = new CreateRideViewModel(_fakeNavigationService, _fakePageDialogService, _fakeBackendApiService);


            _priceResponseOk = new PriceResponse(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new
                {
                    price = 100.00,
                }), Encoding.UTF8, "application/json"),
            });

            _priceResponseBadRequest = new PriceResponse(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent(JsonConvert.SerializeObject(new
                {
                    errors = new Dictionary <string, IList <string> >()
                    {
                        { "error", new List <string> {
                              "The address is not valid"
                          } }
                    },
                }), Encoding.UTF8, "application/json"),
            });

            _rideResponseOk = new CreateRideResponse(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.OK,
                Content    = new StringContent(JsonConvert.SerializeObject(new
                {
                    id = 1,
                    startDestination     = new { cityName = "Test", postalCode = 1234, streetName = "Tester", streetNumber = 1 },
                    endDestination       = new { cityName = "Tester", postalCode = 4321, streetName = "Test", streetNumber = 2 },
                    departureTime        = DateTime.Now.Add(new TimeSpan(0, 0, 30)),
                    confirmationDeadline = DateTime.Now.Subtract(new TimeSpan(0, 2, 0)),
                    passengerCount       = 1,
                    createdOn            = DateTime.Now,
                    price  = 100,
                    status = 0,
                }), Encoding.UTF8, "application/json"),
            });

            _rideResponseBadRequest = new CreateRideResponse(new HttpResponseMessage()
            {
                StatusCode = HttpStatusCode.BadRequest,
                Content    = new StringContent(JsonConvert.SerializeObject(new
                {
                    errors = new Dictionary <string, IList <string> >()
                    {
                        { "error", new List <string> {
                              "Not enough money"
                          } }
                    },
                }), Encoding.UTF8, "application/json"),
            });
        }