Exemplo n.º 1
0
        public async Task ApplyDataAsync_WhenEntitiesIsNullOrEmpty(
            bool enableIdentityInsert,
            IList <FakeEntity> entities)
        {
            // Arrange
            var service = new FakeDataService(
                _mockConfigurationBuilder.Object,
                _mockHostEnvironment.Object,
                FolderPath,
                _mockDbContext.Object,
                enableIdentityInsert,
                x => x.Id,
                _mockLogger.Object
                );

            service.SetData(entities);

            _mockLogger
            .Setup(x => x.IsEnabled(LogLevel.Warning))
            .Returns(true);

            Expression <Action <ILogger> > loggerExpression = x => x.Log(
                LogLevel.Warning,
                0,
                It.Is <It.IsAnyType>((v, t) =>
                                     v.ToString() ==
                                     $"Empty data: {nameof(FakeEntity)}"
                                     ),
                null,
                It.Is <Func <It.IsAnyType, Exception, string> >((v, t) => true)
                );

            _mockLogger.Setup(loggerExpression);

            // Act
            await service.ApplyDataAsync();

            // Assert
            _mockLogger.Verify(loggerExpression, Times.Once);
        }
        public async Task ApplyDataAsync_Success(
            bool enableIdentityInsert,
            Collection <FakeEntity> existingEntities,
            Collection <FakeEntity> entities,
            Collection <FakeEntity> expectedEntities)
        {
            // Arrange
            _dbContext
            .FakeEntities
            .AddRange(existingEntities);

            await _dbContext.SaveChangesAsync();

            _dbContext.ChangeTracker.Clear();

            var service = new FakeDataService(
                _configurationBuilder,
                _hostingEnvironment,
                "Test FolderPath",
                _dbContext,
                enableIdentityInsert,
                x => x.Id,
                _logger
                );

            service.SetData(entities);

            // Act
            await service.ApplyDataAsync();

            // Assert
            Assert.Equal(
                expectedEntities,
                _dbContext.FakeEntities.ToList(),
                new FakeEntityEqualityComparer()
                );
        }