CodeCorrectCollective.Blocks.EntityFramework.Testing
1.0.2-alpha.0.1
Prefix Reserved
See the version list below for details.
dotnet add package CodeCorrectCollective.Blocks.EntityFramework.Testing --version 1.0.2-alpha.0.1
NuGet\Install-Package CodeCorrectCollective.Blocks.EntityFramework.Testing -Version 1.0.2-alpha.0.1
<PackageReference Include="CodeCorrectCollective.Blocks.EntityFramework.Testing" Version="1.0.2-alpha.0.1" />
<PackageVersion Include="CodeCorrectCollective.Blocks.EntityFramework.Testing" Version="1.0.2-alpha.0.1" />
<PackageReference Include="CodeCorrectCollective.Blocks.EntityFramework.Testing" />
paket add CodeCorrectCollective.Blocks.EntityFramework.Testing --version 1.0.2-alpha.0.1
#r "nuget: CodeCorrectCollective.Blocks.EntityFramework.Testing, 1.0.2-alpha.0.1"
#:package CodeCorrectCollective.Blocks.EntityFramework.Testing@1.0.2-alpha.0.1
#addin nuget:?package=CodeCorrectCollective.Blocks.EntityFramework.Testing&version=1.0.2-alpha.0.1&prerelease
#tool nuget:?package=CodeCorrectCollective.Blocks.EntityFramework.Testing&version=1.0.2-alpha.0.1&prerelease
C3.Blocks.EntityFramework.Testing
This library is a simple library to assist with testing EntityFramework libraries against a Sqlite database or some other database type.
The library attempts to be unopinionated in that you can use the EntityFrameworkSqliteTestBase to test against an in-memory sqlite database. You can also create a new subclass of EntityFrameworkTestBase to connect to the database engine of your choice.
Connecting to a custom database engine
- Create a subclass of
EntityFrameworkTestBase
(See EntityFrameworkSqliteTestBase as an example):public abstract class EntityFrameworkSqliteTestBase<TDbContext> : EntityFrameworkTestBase<TDbContext> where TDbContext : DbContext { protected override DbConnection CreateSqlConnection() => new SqliteConnection("Datasource=:memory:"); protected override DbContextOptions<TDbContext> MakeDbContextOptions( DbContextOptionsBuilder<TDbContext> dbContextOptionsBuilder, DbConnection connection) => dbContextOptionsBuilder .UseSqlite(connection) .Options; }
- Modify the above code to create your own
DbConnection
and to return the correctDbConnectionOptions<TDbContext>
Create Unit Tests, Setting up the Database before each Test
The idea is that it is quick to create a new database and migrate the changes to it before each test. This way the tests can be repeatable on each run.
See TestDbContextTests for an example how create a test.
public class TestDbContextTests : EntityFrameworkSqliteTestBase<TestDbContext>
{
[Fact]
public async Task RunTestMethodTest()
{
// Arrange
var d1 = new TestDomainObject { Name = "d1" };
var d2 = new TestDomainObject { Name = "d2" };
var d3 = new TestDomainObject { Name = "d3" };
// Act, Assert
await this.RunTestAsync(
async Task (context, cancellationToken) =>
{
var items = await context.TestDomainObject.ToListAsync(cancellationToken);
// Assert
Assert.Equal(3, items.Count);
Assert.NotNull(items.FirstOrDefault(i => i.Name == "d2"));
},
async Task (context, cancellationToken) =>
{
context.AddRange([d3, d2, d1]);
await context.SaveChangesAsync(cancellationToken);
}
);
}
// Other test remove fro brevity
}
Basic Steps
- The test class should derive from
EntityFrameworkSqliteTest<TDBContext>
whereTDbContext
is theDbcontext
type specific to your project. - In your test method, call the
RunTest
method, passing in the two functions for your test, the first function for the operation to run that is under test and the second function to setup the data required for the operation function.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
-
net9.0
- Microsoft.Data.Sqlite (>= 8.0.12)
- Microsoft.EntityFrameworkCore.Sqlite (>= 8.0.12)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last Updated |
---|---|---|
1.0.7 | 251 | 3/12/2025 |
1.0.7-alpha.0.1 | 138 | 3/12/2025 |
1.0.6 | 165 | 3/12/2025 |
1.0.6-alpha.0.1 | 134 | 3/12/2025 |
1.0.5 | 250 | 2/13/2025 |
1.0.5-alpha.0.3 | 70 | 2/13/2025 |
1.0.4 | 106 | 2/8/2025 |
1.0.3 | 110 | 2/5/2025 |
1.0.3-alpha.0.1 | 72 | 2/5/2025 |
1.0.2 | 195 | 1/31/2025 |
1.0.2-alpha.0.1 | 63 | 1/31/2025 |
1.0.1 | 107 | 1/31/2025 |
Initial Release