EntityFrameworkCore.AutoFixture 2.0.0-preview0003

This is a prerelease version of EntityFrameworkCore.AutoFixture.
There is a newer version of this package available.
See the version list below for details.
dotnet add package EntityFrameworkCore.AutoFixture --version 2.0.0-preview0003                
NuGet\Install-Package EntityFrameworkCore.AutoFixture -Version 2.0.0-preview0003                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="EntityFrameworkCore.AutoFixture" Version="2.0.0-preview0003" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add EntityFrameworkCore.AutoFixture --version 2.0.0-preview0003                
#r "nuget: EntityFrameworkCore.AutoFixture, 2.0.0-preview0003"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install EntityFrameworkCore.AutoFixture as a Cake Addin
#addin nuget:?package=EntityFrameworkCore.AutoFixture&version=2.0.0-preview0003&prerelease

// Install EntityFrameworkCore.AutoFixture as a Cake Tool
#tool nuget:?package=EntityFrameworkCore.AutoFixture&version=2.0.0-preview0003&prerelease                

EntityFrameworkCore.AutoFixture

GitHub Workflow Status Coveralls github Total alerts Nuget 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 // Will 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.<br/> This project is MIT licensed.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on EntityFrameworkCore.AutoFixture:

Package Downloads
JoanComas.ScenarioUnitTesting.AspNetCore

Simplify your controller unit tests with a Scenario<T> class as a single point to access the class under test and its dependencies, leveraging xUnit, AutoFixture and NSubstitute.

GitHub repositories (1)

Showing the top 1 popular GitHub repositories that depend on EntityFrameworkCore.AutoFixture:

Repository Stars
Azure/azure-saas
The Azure SaaS Development Kit (ASDK) provides a reference architecture, deployable reference implementation and tools to help developers, startups, ISVs and Enterprises deliver their applications as a SaaS service. A platform for platform creators.
Version Downloads Last updated
2.1.0 135,400 11/27/2023
2.0.1 104,212 1/20/2023
2.0.0 74,554 8/7/2022
2.0.0-preview0003 767 8/7/2022
2.0.0-preview0002 788 8/4/2022
1.3.0 21,077 3/16/2022
1.2.0 9,197 3/15/2022
1.1.0 15,388 2/17/2022
1.0.0 1,418 12/10/2021