public async void AddLocation_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var locationForCreationDto = new LocationForCreationDto
            {
                Name = "Warsaw"
            };
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.AddLocationAsync(Arg.Any <Location>())
            .Returns(_ =>
            {
                var locationToAdd = _.Arg <Location>();
                locationToAdd.Id  = 55;
                var tcs           = new TaskCompletionSource <Location>();
                tcs.SetResult(locationToAdd);
                return(tcs.Task);
            });

            // Act
            var sit    = new LocationsController(locationDataService, Substitute.For <ILogger <LocationsController> >());
            var result = await sit.AddLocation(locationForCreationDto);

            // Assert
            var addedLocation = WebApiTestsHelper.ExtractObjectFromActionResult <OkObjectResult, LocationDto>(result);

            Assert.Equal(locationForCreationDto.Name, addedLocation.Name);
            Assert.Equal(55, addedLocation.Id);

            WebApiTestsHelper.Unlock();
        }
Exemple #2
0
        public async Task AddLocation_NewLocationRequest_ReturnStatusCodeCreatedAtExpectedRoutName()
        {
            // Arrange
            var locationRequest = new LocationRequest
            {
                VehicleId = 1,
                Latitude  = 13.788571,
                Longitude = 100.538034
            };

            var locationResponse = new LocationResponse
            {
                Id        = Guid.NewGuid(),
                VehicleId = 1,
                Latitude  = 13.788571,
                Longitude = 100.538034,
            };

            locationsService
            .AddLocationAsync(Arg.Any <LocationRequest>())
            .Returns(Task.FromResult(locationResponse));

            // Act
            var actionResult = await locationsController.AddLocation(locationRequest);

            // Assert
            var requestResult = Assert.IsType <OkObjectResult>(actionResult);

            Assert.Equal((int)System.Net.HttpStatusCode.OK, requestResult.StatusCode);
        }
        public async void AddLocation_ShouldReturnBadRequest()
        {
            // Arrange
            var locationForCreationDto = new LocationForCreationDto();

            // Act
            var sit = new LocationsController(null, Substitute.For <ILogger <LocationsController> >());

            sit.ModelState.AddModelError("PropName", "ModelError");
            var result = await sit.AddLocation(locationForCreationDto);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
Exemple #4
0
        public async Task AddLocation_LocationViewModelInValid_ReturnsBadRequest()
        {
            // Arrange
            ILocationService    service    = Substitute.For <ILocationService>();
            LocationsController controller = GetController(locationService: service);

            controller.ModelState.AddModelError("Name", "Required");
            AddLocationViewModel viewModel = new AddLocationViewModel();

            // Act
            IActionResult result = await controller.AddLocation(viewModel);

            // Assert
            Assert.IsType <BadRequestObjectResult>(result);
        }
Exemple #5
0
        public async Task AddLocation_LocationViewModel_AddOnServiceHitWithMappedLocation()
        {
            // Arrange
            ILocationService     service    = Substitute.For <ILocationService>();
            LocationsController  controller = GetController(locationService: service);
            AddLocationViewModel viewModel  = new AddLocationViewModel {
                Name = "Contoso", Address = "Seattle", ZipOrPostcode = "54321"
            };

            // Act
            IActionResult result = await controller.AddLocation(viewModel);

            // Assert
#pragma warning disable CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            service.Received(1).Add(Arg.Any <Location>());
#pragma warning restore CS4014 // Because this call is not awaited, execution of the current method continues before the call is completed
            Assert.IsType <RedirectResult>(result);
        }