TestingFixtures.FileBasedContextFactory
0.1.0
There is a newer version of this package available.
See the version list below for details.
See the version list below for details.
dotnet add package TestingFixtures.FileBasedContextFactory --version 0.1.0
NuGet\Install-Package TestingFixtures.FileBasedContextFactory -Version 0.1.0
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="TestingFixtures.FileBasedContextFactory" Version="0.1.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TestingFixtures.FileBasedContextFactory --version 0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: TestingFixtures.FileBasedContextFactory, 0.1.0"
#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 TestingFixtures.FileBasedContextFactory as a Cake Addin #addin nuget:?package=TestingFixtures.FileBasedContextFactory&version=0.1.0 // Install TestingFixtures.FileBasedContextFactory as a Cake Tool #tool nuget:?package=TestingFixtures.FileBasedContextFactory&version=0.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
TestingFixture
Quickly setup tests for your .NET-EntityFrameWorkCore classes that use DbContext directly or IDbContextFactory<DbContext> directly.
Repository
Benefits
- Access real databases in your UnitTests by using Sqlite-via-Files or Postgresql-via-Docker.
- Both Sqlite and TestContainers will run in CI-CD-Pipelines (as tests-step in this project's workflow should show)
- The InMemoryDb keeps Items and References in context longer than in production and might introduce sublte Bugs in your Unit Tests.
- Sqlite is still really fast, even compared to the InMemoryDb-Variant
- Postgres via Docker will create a real Database as used in production. So even extra logic like triggers, custom-database-functions etc. can be tested.
Usage
Examples
- For the no-setup reflection based .New() function to work your custom DbContext is expected
to have an constructor taking in soley
(DbContextOptions options)
or(DbContextOptions<MyDbContext> options)
public class MyDbContext : DbContext
{
public CustomDbContext(DbContextOptions options) : base(options)
{
}
public DbSet<Article> Articles { get; set; }
protected override void OnModelCreating(ModelBuilder modelBuilder)
{
base.OnModelCreating(modelBuilder);
modelBuilder.Entity<Article>().HasData(
new Article
{
Ean = "16556324",
Title = "Sound absorbing dog bed",
},
new Article
{
Ean = "80295631",
Title = "Birdhouse Wood",
}
);
}
public record Article
{
[Key] public string Ean { get; set; }
public string Title { get; set; }
}
}
- We have some Repository or Process that directly accesses the db. That we intend to test directly or in an combined integration test scenario.
public class SomeProcessWithDbAccess(IDbContextFactory<MyDbContext> _contextFactory)
{
public async Task AddArticle(ArticleDto article)
{
await using var ctx = await _contextFactory.CreateDbContextAsync();
ctx.Articles.Add(new Article
{
Ean = article.Title,
Title = article.Ean,
})
await ctx.SaveChangesAsync();
}
}
- Using The
FileBasedContextFactory<TCtx>
or thePostgresDockerContextFactory<TCtx>
it is possible to directly test against a real database. Independent of Nunit.
public class SomeProcessWithDbAccessTests
{
[Test]
public async Task Article_CorrectlyAdded()
{
// Arrange
using var contextFactory = await FileBasedContextFactory<MyDbcontext>.New();
var articleToAdd = new ArticleDto{ Ean = "22222222", Title = "Pair of wool gloves, red"}
// Act
new SomeProcessWithDbAccess(contextFactory).AddArticle(articleToAdd);
// Assert
contextFactory.CreateDbContext().Articles
.Should().ContainSingle(a => a.Ean == "22222222" && Title == "Pair of wool gloves, red" )
}
}
- Using Nunit there is the convenience implementation of a TestFixture:
public class SomeProcessWithDbAccessTests : FileBasedTestFixture<MyDbContext>
{
[Test]
public async Task Article_CorrectlyAdded()
{
// Arrange
var articleToAdd = new ArticleDto{ Ean = "22222222", Title = "Pair of wool gloves, red"}
// Act
new SomeProcessWithDbAccess(ContextFactory).AddArticle(articleToAdd);
// Assert
ContextFactory.CreateDbContext().Articles
.Should().ContainSingle(a => a.Ean == "22222222" && Title == "Pair of wool gloves, red" )
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
net8.0
- Microsoft.EntityFrameworkCore (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.8)
- Microsoft.EntityFrameworkCore.Sqlite.Core (>= 8.0.8)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on TestingFixtures.FileBasedContextFactory:
Package | Downloads |
---|---|
TestingFixtures.FileBasedTestFixture
Generic-ContextFactory to test DbContext or IDbContextFactory directly with Sqlite. |
GitHub repositories
This package is not used by any popular GitHub repositories.