All Projects → aivascu → EntityFrameworkCore.AutoFixture

aivascu / EntityFrameworkCore.AutoFixture

Licence: MIT license
A library aimed to minimize the boilerplate required to unit-test Entity Framework Core code using AutoFixture and in-memory providers.

Programming Languages

C#
18002 projects
powershell
5483 projects
shell
77523 projects
Batchfile
5799 projects

Projects that are alternatives of or similar to EntityFrameworkCore.AutoFixture

automock
A library for testing classes with auto mocking capabilities using jest-mock-extended
Stars: ✭ 26 (-16.13%)
Mutual labels:  unit-testing, mocking, unit-test
Dotnetlabs
.NET Labs -- Show Me the Tips and Tricks and Code
Stars: ✭ 135 (+335.48%)
Mutual labels:  unit-testing, entity-framework, entity-framework-core
Onion Architecture Asp.net Core
WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!
Stars: ✭ 196 (+532.26%)
Mutual labels:  unit-testing, entity-framework, entity-framework-core
unittest expander
A library that provides flexible and easy-to-use tools to parameterize Python unit tests, especially those based on unittest.TestCase.
Stars: ✭ 12 (-61.29%)
Mutual labels:  unit-testing, tests, unit-test
Cmockery
A lightweight library to simplify and generalize the process of writing unit tests for C applications.
Stars: ✭ 697 (+2148.39%)
Mutual labels:  unit-testing, mocking, tests
Cuckoo
Boilerplate-free mocking framework for Swift!
Stars: ✭ 1,344 (+4235.48%)
Mutual labels:  unit-testing, mocking
Js Unit Testing Guide
📙 A guide to unit testing in Javascript
Stars: ✭ 1,346 (+4241.94%)
Mutual labels:  unit-testing, unit-test
Automation Arsenal
Curated list of popular Java and Kotlin frameworks, libraries and tools related to software testing, quality assurance and adjacent processes automation.
Stars: ✭ 105 (+238.71%)
Mutual labels:  unit-testing, mocking
Spring Data Mock
Mock facility for Spring Data repositories
Stars: ✭ 110 (+254.84%)
Mutual labels:  unit-testing, mocking
Mockstar
Demo project on How to be a Mockstar using Mockito and MockWebServer.
Stars: ✭ 53 (+70.97%)
Mutual labels:  unit-testing, mocking
Moderncppci
This is an example of doing a Modern C++ project with CI
Stars: ✭ 109 (+251.61%)
Mutual labels:  unit-testing, unit-test
Kotlinmvparchitecture
Clean MVP Architecture with Dagger2 + Retrofit2 + Mockito + Fresco + EasiestGenericRecyclerAdapter using Kotlin. Added Unit Tests(Kotlin Tests)!
Stars: ✭ 143 (+361.29%)
Mutual labels:  unit-testing, unit-test
Kotlinrxmvparchitecture
Clean MVP Architecture with RxJava + Dagger2 + Retrofit2 + Mockito + Fresco + EasiestGenericRecyclerAdapter using Kotlin. Includes Unit Tests(Kotlin Tests)!
Stars: ✭ 94 (+203.23%)
Mutual labels:  unit-testing, unit-test
Tcunit
An unit testing framework for Beckhoff's TwinCAT 3
Stars: ✭ 74 (+138.71%)
Mutual labels:  unit-testing, unit-test
Unit Threaded
Advanced unit test framework for D
Stars: ✭ 100 (+222.58%)
Mutual labels:  unit-testing, mocking
Vcr
Record HTTP calls and replay them
Stars: ✭ 54 (+74.19%)
Mutual labels:  unit-testing, mocking
generic-for-core
🏗️ Generic Repository & UOW Pattern For ASP.NET Core
Stars: ✭ 55 (+77.42%)
Mutual labels:  entity-framework-core, unit-test
Acutest
Simple header-only C/C++ unit testing facility.
Stars: ✭ 170 (+448.39%)
Mutual labels:  unit-testing, tests
Blazor-CRUD
Simple CRUD application using C#/Blazor
Stars: ✭ 25 (-19.35%)
Mutual labels:  entity-framework, entity-framework-core
Capture Stream
Capture stream output.
Stars: ✭ 10 (-67.74%)
Mutual labels:  unit-testing, tests

EntityFrameworkCore.AutoFixture

GitHub Workflow Status Coveralls github NuGet NuGet downloads GitHub

EntityFrameworkCore.AutoFixture is a library that helps with testing code that uses Entity Framework, by reducing the boilerplate code necessary to set up database contexts (see examples), with the help of AutoFixture.

Unlike other libraries for faking EF contexts, EntityFrameworkCore.AutoFixture does not use mocking frameworks or dynamic proxies in to create database contexts, instead it uses the actual database providers. This ensures the tests will behave a lot more similar to the code in the production environment, with little to no effort.

⚠️ .NET Standard 2.0 in EF Core v3.0.x ⚠️

Entity Framework Core v3.0.0 - v3.0.3 are targeting netstandard2.1, which means they are not compatible with target frameworks that support at most netstandard2.0 (>= net47 and netcoreapp2.1). Versions after v3.1 are targeting netstandard2.0. If you've encountered this issue consider upgrading to a later version of Entity Framework Core.

Features

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

  • InMemoryCustomization - Customizes fixtures to create contexts using the In-Memory database provider
  • SqliteCustomization - Customizes fixtures to create contexts using the SQLite database provider
  • DbContextCustomization - A base class for database provider customizations. Can be used, in more advanced scenarios, for example, to extend the existing functionality or create customizations for other providers.

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 the customizations.

Using In-Memory database provider

By default this customization will configure all contexts to use the in-memory database provider for Enity Framework, and will create the database, giving you a ready to use context.

[Fact]
public async Task CanSavesCustomers()
{
    // Arrange
    var fixture = new Fixture().Customize(new InMemoryCustomization());
    var context = fixture.Create<TestDbContext>();

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

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

With the default configuration, the custmization will set the store name to TestDatabase and will suffix it with a random string to ensure the name is unique. After the context is created it will run Database.EnsureCreated() to create the database.

This behavior can be changed by setting the corresponding values in the customizaiton initializer.

[Fact]
public async Task CanSavesCustomers()
{
    // Arrange
    var fixture = new Fixture().Customize(new InMemoryCustomization
    {
        DatabaseName = "MyCoolDatabase", // Sets the store name to "MyCoolDatabase"
        UseUniqueNames = false, // No suffix for store names. All contexts will connect to same store
        OnCreate = OnCreateAction.Migrate // Will run Database.Migrate()
                                          // Use OnCreateAction.None to skip creating the database
    });
    var context = fixture.Create<TestDbContext>();

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

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

To encapsulate the configuration and remove even more of the boilerplate, use the AutoData attributes offered by AutoFixture.

public class PersistenceDataAttribute : AutoDataAttribute
{
    public PersistenceDataAttribute()
        : base(() => new Fixture().Customize(new InMemoryCustomization {
            UseUniqueNames = false,
            OnCreate = OnCreateAction.Migrate
        }))
    {
    }
}
[Theory, PersistenceData] // Notice the data attribute
public async Task CanUseGeneratedContext(TestDbContext context)
{
    // Arrange & Act
    context.Customers.Add(new Customer("Jane Smith"));
    await context.SaveChangesAsync();

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

Using SQLite database provider

By default this customization will configure all contexts to use the SQLite database provider for Enity Framework, and will automatically create the database, giving you a ready to use context.

[Fact]
public async Task CanUseGeneratedContext()
{
    // Arrange
    var fixture = new Fixture().Customize(new SqliteCustomization());
    var context = fixture.Create<TestDbContext>();

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

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

With the default configuration, the custmization will set the connection string to Data Source=:memory:, will open the connection and after the context is created it will run Database.EnsureCreated() to create the database.

This behavior can be changed by setting the corresponding values in the customizaiton initializer.

[Fact]
public async Task CanSavesCustomers()
{
    // Arrange
    var fixture = new Fixture().Customize(new SqliteCustomization
    {
        ConnectionString = "Data Source=MyDatabase.sqlite;Cache=Shared;", // Sets the connection string to connect to a file
        AutoOpenConnection = false, // Disables opening the connection by default. Affects all SqliteConnection instances.
        OnCreate = OnCreateAction.None // Use OnCreateAction.None to skip creating the database.
                                       // Use OnCreateAction.EnsureCreated to run Database.EnsureCreated() automatically
                                       // Use OnCreateAction.Migrate to run Database.Migrate() automatically
    });
    var connection = fixture.Freeze<SqliteConnection>();
    var context = fixture.Create<TestDbContext>();
    connection.Open();
    context.Database.Migrate();

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

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

To encapsulate the configuration and remove even more of the boilerplate, use the AutoData attributes offered by AutoFixture.

public class PersistenceDataAttribute : AutoDataAttribute
{
    public PersistenceDataAttribute()
        : base(() => new Fixture().Customize(new SqliteCustomization {
            ConnectionString = "Data Source=MyDatabase;Mode=Memory;Cache=Shared;"
            OnCreate = OnCreateAction.Migrate
        }))
    {
    }
}
[Theory, PersistenceData] // Notice the data attribute
public async Task CanUseGeneratedContext(TestDbContext context)
{
    // Arrange & Act
    context.Customers.Add(new Customer("Jane Smith"));
    await context.SaveChangesAsync();

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

License

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

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].