public async Task Building_CompleteGraph_AndSaving_PersistsGraph()
        {
            // Arrange
            var individualBuilder = _entityBuilderFactory.Create <IndividualBuilder>();

            // Act
            var createdIndividual = await individualBuilder
                                    .WithAddress(
                config =>
            {
                config.WithCity();
                config.WithCity();
            })
                                    .WithAddress()
                                    .BuildAsync(true);

            // Assert
            await using var dbContext = _dbContextFactory.Create();
            var actualIndividual = await dbContext.Set <Individual>()
                                   .Include(f => f.Addresses)
                                   .ThenInclude(f => f.Streets)
                                   .SingleAsync(f => f.Id == createdIndividual.Id);

            Assert.AreEqual(IndividualBuilder.DefaultBirthdate, actualIndividual.Birthdate);
            Assert.AreEqual(IndividualBuilder.DefaultFirstName, actualIndividual.FirstName);
            Assert.AreEqual(IndividualBuilder.DefaultLastName, actualIndividual.LastName);

            var addresses = actualIndividual.Addresses;

            Assert.AreEqual(2, addresses.Count);
            Assert.IsTrue(addresses.All(adr => adr.Zip == AddressBuilder.DefaultZip));
            Assert.IsTrue(addresses.All(adr => adr.City == AddressBuilder.DefaultCity));

            var firstAddress = addresses.First();

            Assert.AreEqual(2, firstAddress.Streets.Count);
            Assert.IsTrue(firstAddress.Streets.All(str => str.StreetName == StreetBuilder.DefaultStreetName));
            Assert.IsTrue(firstAddress.Streets.All(str => str.StreetNumber == StreetBuilder.DefaultStreetNumber));
        }
Exemple #2
0
        public async Task <TEntity> BuildAsync(bool doSave = false)
        {
            await CreateNavigationEntitiesAsync();

            if (!doSave)
            {
                return(Entity);
            }

            await using var appContext = _appContextFactory.Create();
            appContext.Add(Entity);
            await appContext.SaveChangesAsync();

            return(Entity);
        }