JonPSmith / EfCore.TestSupport

Tools for helping in unit testing applications that use Entity Framework Core
https://www.thereformedprogrammer.net/new-features-for-unit-testing-your-entity-framework-core-5-code/
Other
352 stars 53 forks source link

Need to init SQLite on full .Net Framework #6

Closed CrahunGit closed 6 years ago

CrahunGit commented 6 years ago

In .Net 4.7.1 you need to call the SQLitePCL.Batteries.Init() on the test initializer. In net core it works as expected.

JonPSmith commented 6 years ago

Hi @CrahunGit,

Thanks for letting me know. I have to admit I haven't used EF Core with .NET 4.7.1.

Can you add some example code to show what you need to do. I have linked this issue into the SQLite wiki page of this project.

CrahunGit commented 6 years ago

Imagine a simple test like this:

public class EntityServiceTest
    {
        public EntityServiceTest()
        {
          //You need to call this on every test to work. If not exception tells you to call it
            SQLitePCL.Batteries.Init();
        }

        [Fact]
        public async Task GetEntities_AllActiveEntities_Returns1Entity()
        {
            //Arrange
            DbContextOptions<EntidadContext> options = SqliteInMemory.CreateOptions<EntidadContext>();
            Entity entity1 = new Entity { Id = 1, Name = "Entity 1", Active = "A"};
            Entity entity2 = new Entity { Id = 2, Name = "Entity 2", Active = "D"};

            using (EntityContext context = new EntityContext(options))
            {
                await SeedRangeDatabaseSameContext(context, new List<Entity>{ entity1, entity2 });

                EntityRepository repository = new EntityRepository(context);
                EntityService service = new EntityService(context, repository);

                //Act
                List<DtoEntity> entities = await service.GetEntities();

                //Assert
                entities.Should().HaveCount(1);
            }
        }
}

As the comment says you need to call init for the sqlite on every context using sqlite.

JonPSmith commented 6 years ago

Thanks @CrahunGit - very helpful. I'm going to close this issue.