Code Monkey home page Code Monkey logo

entityframeworkcore.autofixture's Introduction

EntityFrameworkCore.AutoFixture

GitHub Workflow Status Coveralls github Total alerts Nuget GitHub

EntityFrameworkCore.AutoFixture is the logical product of Entity Framework Core in-memory providers and AutoFixture.

Using EntityFrameworkCore.AutoFixture you can greatly reduce the boilerplate work necessary to unit test code that uses Entity Framework Core database contexts (see examples). You'll appreciate this library if you are already using AutoFixture as your auto-mocking container.

EntityFrameworkCore.AutoFixture extens AutoFixture with the ability to create fully functional DbContext instances, with very little setup code.

Unlike other libraries for faking EF contexts, EntityFrameworkCore.AutoFixture does not use mocking frameworks or dynamic proxies in order to create DbContext instances, instead it uses the Microsoft's own in-memory providers for EF Core. This allows to make less assumptions (read as: mock setups) in your tests about how the DbContext will behave in the real environment.

Features

EntityFrameworkCore.AutoFixture offers three customizations to aid your unit testing workflow:

  • InMemoryContextCustomization - customizes fixtures to use the In-Memory database provider when creating DbContext instances
  • SqliteContextCustomization - customizes fixtures to use the SQLite database provider when creating DbContext instances. By default the customization will create contexts for an in-memory connection string (i.e. DataSource=:memory:). This can be changed by providing the fixture a predefined SqliteConnection instance.
  • DbContextCustomization - serves as the base customization for the other two implementations. The customization can be used, in more advanced scenarios, when you want to extend the fixtures with your own specimen builders.

Examples

The examples below demonstrate, the possible ways of using the library in xUnit test projects, both with [Fact] and [Theory] tests.

The library is not limited to xUnit and can be used with other testing frameworks like NUnit and MSTest, since it only provides a few Customization implementations.

Using In-Memory database provider

[Fact]
public void SaveChanges_ShouldCreateCustomerRecord()
{
    var fixture = new Fixture().Customize(new InMemoryContextCustomization());
    using (var context = fixture.Create<TestDbContext>())
    {
        context.Database.EnsureCreated();

        context.Customers.Add(new Customer("John Doe"));
        context.SaveChanges();

        context.Customers.Should().Contain(x => x.Name == "John Doe");
    }
}

The next example uses a custom AutoData attribute AutoDomainDataWithInMemoryContext that customizes the fixture with the same customization as in the example above. This helps abstract away even more setup code. The attribute implementation can be found the sources of the test projects.

[Theory]
[AutoDomainDataWithInMemoryContext]
public async Task SaveChangesAsync_ShouldCreateCustomerRecord(TestDbContext context)
{
    await using (context)
    {
        await context.Database.EnsureCreatedAsync();

        context.Customers.Add(new Customer("Jane Smith"));
        await context.SaveChangesAsync();

        context.Customers.Should().Contain(x => x.Name == "Jane Smith");
    }
}

Using SQLite database provider

When using the SQLite database provider be sure to also freeze / inject the SqliteConnection instance, in order to be able to control its lifetime. Otherwise the connection might close, which might in its turn fail your tests.

[Theory]
[AutoDomainDataWithSqliteContext]
public void Customize_ShouldProvideSqliteContext([Frozen] SqliteConnection connection,
  TestDbContext context, Item item, Customer customer)
{
    using (connection)
    using (context)
    {
        connection.Open();
        context.Database.EnsureCreated();
        context.Items.Add(item);

        context.Customers.Add(customer);
        context.SaveChanges();

        customer.Order(item, 5);
        context.SaveChanges();

        context.Orders.Should().Contain(x => x.CustomerId == customer.Id && x.ItemId == item.Id);
    }
}

License

Copyright © 2019 Andrei Ivascu.
This project is MIT licensed.

entityframeworkcore.autofixture's People

Contributors

aivascu avatar dependabot-preview[bot] avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.