예제 #1
0
        public async Task EditAsyncThrowsWhenTheInputModelIsNull()
        {
            // Arrange
            var mapperMock     = new Mock <IMapper>();
            var repositoryMock = new Mock <IDeletableEntityRepository <Country> >();

            var countriesService = new CountriesService(repositoryMock.Object, mapperMock.Object);

            // Assert
            await Assert.ThrowsAsync <ArgumentNullException>(async() =>
            {
                // Act
                await countriesService.EditAsync("validId", null, new ImageInputModel());
            });
        }
        public async Task EditAsync_ShouldReturnCorrectResult()
        {
            MapperInitializer.InitializeMapper();
            var context             = ApplicationDbContextInMemoryFactory.InitializeContext();
            var countriesRepository = new EfDeletableEntityRepository <Country>(context);
            var countriesService    = new CountriesService(countriesRepository);
            var seeder = new DbContextTestsSeeder();
            await seeder.SeedCountriesAsync(context);

            var countryFromDb = await countriesRepository.All().FirstOrDefaultAsync();

            var country = countryFromDb.To <CountryServiceDetailsModel>();

            country.Name = "BGN";
            var result = await countriesService.EditAsync(country);

            Assert.True(result == 1, ErrorMessage);
        }
예제 #3
0
        public async Task EditAsyncEditsTheCorrectPropertiesAndSavesTheResult(
            string countryName,
            string description,
            string countryCode,
            string newCountryName,
            string newDescription,
            string newCountryCode,
            bool imageNull,
            string imageSource,
            string newImageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);
            var saved = true;

            var country = new Country()
            {
                Name        = countryName,
                CountryCode = countryCode,
                Description = description,
                Image       = new Image()
                {
                    Source = imageSource,
                },
            };

            var countriesList = new List <Country>()
            {
                new Country(),
                new Country(),
                country,
                new Country(),
            };

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

            repositoryMock.Setup(x => x.All())
            .Returns(countriesList.AsQueryable());

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

            var countriesService = new CountriesService(repositoryMock.Object, AutoMapperConfig.MapperInstance);

            var editInputModel = new CountryInputModel()
            {
                Name        = newCountryName,
                CountryCode = newCountryCode,
                Description = newDescription,
            };

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

            if (imageNull)
            {
                imageInputModel = null;
            }

            // Act
            await countriesService.EditAsync(country.Id, editInputModel, imageInputModel);

            // Assert
            Assert.True(saved);

            Assert.Equal(newCountryName, country.Name);
            Assert.Equal(newCountryCode, country.CountryCode);
            Assert.Equal(newDescription, country.Description);

            var actualImage = country.Image;

            if (imageNull)
            {
                Assert.Equal(imageSource, actualImage.Source);
            }
            else
            {
                Assert.Equal(newImageSource, actualImage.Source);
            }
        }