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());
        }
Example #2
0
        public async Task AddAsyncThrowsWhenTheInputModelIsNull()
        {
            // 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.AddAsync(null, new ImageInputModel());
            });
        }
Example #3
0
        public async Task AddMapsTheInputModelAndImageAndAddsThem(
            string name,
            string description,
            string countryCode,
            bool nullImage,
            string imageSource)
        {
            // Arrange
            AutoMapperConfig.RegisterMappings(typeof(Test).Assembly, typeof(ErrorViewModel).Assembly);

            var saved         = true;
            var countriesList = new List <Country>();

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

            repositoryMock.Setup(x => x.AddAsync(It.IsAny <Country>()))
            .Callback((Country country) =>
            {
                countriesList.Add(country);
            });

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

            var cityInputModel = new CountryInputModel()
            {
                Name        = name,
                CountryCode = countryCode,
                Description = description,
            };

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

            if (nullImage)
            {
                imageInputModel = null;
            }

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

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

            // Assert
            var actualCountry = countriesList[0];

            Assert.True(saved);
            Assert.Equal(name, actualCountry.Name);
            Assert.Equal(description, actualCountry.Description);
            Assert.Equal(countryCode, actualCountry.CountryCode);

            var actualImage = actualCountry.Image;

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