public async Task AddAsyncShouldThrowNullException(string name)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAsync").Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <City>(dbContext);
            var service    = new CitiesService(repository);
            await Assert.ThrowsAsync <ArgumentNullException>(() => service.AddAsync(name));
        }
示例#2
0
        public async Task AddAsync_AddsEntity()
        {
            //Assert

            //Act
            await testedService.AddAsync(newEntity);

            //Assert
            Mock.Verify(c => c.Add(newEntity), Times.Once());
            Mock.Verify(c => c.SaveChangesAsync(It.IsAny <CancellationToken>()), Times.Once());
        }
        public async Task AddAsyncShouldWork(string name)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "AddAsync").Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <City>(dbContext);
            var service    = new CitiesService(repository);
            await service.AddAsync(name);

            Assert.True(repository.All().Any(x => x.Name == name));
        }
        public async Task GetByNameShouldWork(string name)
        {
            var options = new DbContextOptionsBuilder <ApplicationDbContext>()
                          .UseInMemoryDatabase(databaseName: "GetByName").Options;
            var dbContext = new ApplicationDbContext(options);

            var repository = new EfDeletableEntityRepository <City>(dbContext);
            var service    = new CitiesService(repository);
            await service.AddAsync(name);

            Assert.Equal(name, service.GetCityByName(name).Name);
        }
示例#5
0
        public async Task AddAsyncThrowsWhenTheInputModelIsNull()
        {
            // Arrange
            var mapperMock     = new Mock <IMapper>();
            var repositoryMock = new Mock <IDeletableEntityRepository <City> >();

            var citiesService = new CitiesService(repositoryMock.Object, mapperMock.Object);

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                // Act
                await citiesService.AddAsync(null, new ImageInputModel());
            });
        }
示例#6
0
        public async Task AddMapsTheInputModelAndImageAndAddsThem(
            string name,
            string description,
            string countryId,
            bool nullImage,
            string imageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved      = true;
            var citiesList = new List <City>();

            var repositoryMock = new Mock <IDeletableEntityRepository <City> >();

            repositoryMock.Setup(x => x.AddAsync(It.IsAny <City>()))
            .Callback((City city) =>
            {
                citiesList.Add(city);
            });

            repositoryMock.Setup(x => x.SaveChangesAsync())
            .Callback(() =>
            {
                saved = true;
            });

            var cityInputModel = new CityInputModel()
            {
                Name        = name,
                Description = description,
                CountryId   = countryId,
            };

            var imageInputModel = new ImageInputModel()
            {
                Source = imageSource,
            };

            if (nullImage)
            {
                imageInputModel = null;
            }

            var citiesService = new CitiesService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            // Act
            await citiesService.AddAsync(cityInputModel, imageInputModel);

            // Assert
            var actualCity = citiesList[0];

            Assert.True(saved);
            Assert.Equal(name, actualCity.Name);
            Assert.Equal(description, actualCity.Description);
            Assert.Equal(countryId, actualCity.CountryId);

            var actualImage = actualCity.Image;

            if (nullImage)
            {
                Assert.Null(actualImage);
            }
            else
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
        }