EntityFrameworkCore.AutoFixture
2.0.0-preview0002
See the version list below for details.
dotnet add package EntityFrameworkCore.AutoFixture --version 2.0.0-preview0002
NuGet\Install-Package EntityFrameworkCore.AutoFixture -Version 2.0.0-preview0002
<PackageReference Include="EntityFrameworkCore.AutoFixture" Version="2.0.0-preview0002" />
paket add EntityFrameworkCore.AutoFixture --version 2.0.0-preview0002
#r "nuget: EntityFrameworkCore.AutoFixture, 2.0.0-preview0002"
// Install EntityFrameworkCore.AutoFixture as a Cake Addin #addin nuget:?package=EntityFrameworkCore.AutoFixture&version=2.0.0-preview0002&prerelease // Install EntityFrameworkCore.AutoFixture as a Cake Tool #tool nuget:?package=EntityFrameworkCore.AutoFixture&version=2.0.0-preview0002&prerelease
EntityFrameworkCore.AutoFixture
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.
⚠️ .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 aid your unit testing workflow:
InMemoryContextCustomization
- customizes fixtures to use the In-Memory database provider when creating DbContext instancesSqliteContextCustomization
- 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 predefinedSqliteConnection
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 async Task CanUseGeneratedContext()
{
// Arrange
var fixture = new Fixture().Customize(new InMemoryContextCustomization
{
AutoCreateDatabase = true,
OmitDbSets = true
});
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");
}
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.
[Theory, InMemoryData]
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");
}
The attribute used in the test above might look something like the following.
public class InMemoryDataAttribute : AutoDataAttribute
{
public InMemoryDataAttribute()
: base(() => new Fixture()
.Customize(new InMemoryContextCustomization
{
OmitDbSets = true,
AutoCreateDatabase = true
}))
{
}
}
Using SQLite database provider
When using the SQLite database provider there is another configuration available in the customization,
called AutoOpenConnection
which allows to automatically open database connections, after they are resolved from the
fixture. The option is turned off by default in v1, but might become the default in next major releases.
[Fact]
public async Task CanUseGeneratedContext()
{
// Arrange
var fixture = new Fixture()
.Customize(new SqliteContextCustomization
{
AutoCreateDatabase = true,
AutoOpenConnection = true,
OmitDbSets = true
});
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");
}
The same test can be written like this, by using a custom data attribute.
[Theory, SqliteData]
public void CanUseResolvedContextInstance(TestDbContext context)
{
// Arrange & Act
context.Customers.Add(new Customer("Jane Smith"));
context.SaveChanges();
// Assert
context.Customers.Should().Contain(x => x.Name == "Jane Smith");
}
public class SqliteDataAttribute : AutoDataAttribute
{
public SqliteDataAttribute()
: base(() => new Fixture()
.Customize(new SqliteContextCustomization
{
OmitDbSets = true,
AutoOpenConnection = true,
AutoCreateDatabase = true
}))
{
}
}
License
Copyright © 2019 Andrei Ivascu.<br/> This project is MIT licensed.
Product | Versions 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. |
-
.NETStandard 2.0
- AutoFixture (>= 4.4.0)
- Microsoft.Data.Sqlite.Core (>= 2.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore (>= 2.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore.Analyzers (>= 2.1.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 2.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 2.0.0 && < 5.0.0)
-
.NETStandard 2.1
- AutoFixture (>= 4.4.0)
- Microsoft.Data.Sqlite.Core (>= 3.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore (>= 3.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore.Analyzers (>= 3.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 3.0.0 && < 5.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 3.0.0 && < 5.0.0)
-
net5.0
- AutoFixture (>= 4.4.0)
- Microsoft.Data.Sqlite.Core (>= 5.0.0)
- Microsoft.EntityFrameworkCore (>= 5.0.0)
- Microsoft.EntityFrameworkCore.Analyzers (>= 5.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 5.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 5.0.0)
-
net6.0
- AutoFixture (>= 4.4.0)
- Microsoft.Data.Sqlite.Core (>= 6.0.0)
- Microsoft.EntityFrameworkCore (>= 6.0.0)
- Microsoft.EntityFrameworkCore.Analyzers (>= 6.0.0)
- Microsoft.EntityFrameworkCore.InMemory (>= 6.0.0)
- Microsoft.EntityFrameworkCore.Sqlite (>= 6.0.0)
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 |