Esempio n. 1
0
        public void CannotDeleteLocationNotInDatabase()
        {
            // Arrange Everything We Need For Our Unit Tests
            options = new DbContextOptionsBuilder <ApplicationDBContext>()
                      .UseInMemoryDatabase(databaseName: "TestRevatureHousingData")
                      .Options;
            testContext            = new ApplicationDBContext(options);
            dummyLocationsData     = new LocationDummyData();
            dummyLocations         = dummyLocationsData.LocationsList;
            testLocationRepository = new LocationRepository(testContext);
            testLocationController = new LocationsController(testLocationRepository);
            dummyConstantLocation  = new Location()
            {
                LocationID = 3
            };
            //Arrange
            Populate();
            //Act
            var deleteResult     = testLocationController.DeleteLocation(12728).Result.Result;
            var deleteStatusCode = (deleteResult as StatusCodeResult);

            //Assert
            Assert.IsInstanceOfType(deleteResult, typeof(NotFoundResult));
            Assert.AreEqual(deleteStatusCode.StatusCode, 404);
            // Clearing Changes
            ClearAllChanges();
        }
        public async void DeleteLocation_ShouldReturnOk()
        {
            WebApiTestsHelper.Lock();

            // Arrange
            SetUp();
            var locations = new List <Location>
            {
                new Location
                {
                    Id   = 5,
                    Name = "Warsaw"
                }
            };
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.GetAllLocationsAsync()
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <Location> >();
                tcs.SetResult(locations);
                return(tcs.Task);
            });
            locationDataService.RemoveLocationAsync(Arg.Any <int>())
            .Returns(_ =>
            {
                var locationId       = _.Arg <int>();
                var locationToDelete = locations.First(loc => loc.Id == locationId);

                var tcs = new TaskCompletionSource <Location>();
                tcs.SetResult(locationToDelete);
                return(tcs.Task);
            });

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

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

            Assert.Equal(5, deletedLocation.Id);

            WebApiTestsHelper.Unlock();
        }
        public async void DeleteLocation_ShouldReturnNotFound()
        {
            // Arrange
            var locationDataService = Substitute.For <ILocationDataService>();

            locationDataService.GetAllLocationsAsync()
            .Returns(_ =>
            {
                var tcs = new TaskCompletionSource <IEnumerable <Location> >();
                tcs.SetResult(Enumerable.Empty <Location>());
                return(tcs.Task);
            });

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

            // Assert
            Assert.IsType <NotFoundResult>(result);
        }