CodeCorrectCollective.Blocks.EntityFramework.Testing 1.0.2-alpha.0.1

Prefix Reserved
This is a prerelease version of CodeCorrectCollective.Blocks.EntityFramework.Testing.
There is a newer version of this package available.
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
                    
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="CodeCorrectCollective.Blocks.EntityFramework.Testing" Version="1.0.2-alpha.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="CodeCorrectCollective.Blocks.EntityFramework.Testing" Version="1.0.2-alpha.0.1" />
                    
Directory.Packages.props
<PackageReference Include="CodeCorrectCollective.Blocks.EntityFramework.Testing" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
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"
                    
#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.
#:package CodeCorrectCollective.Blocks.EntityFramework.Testing@1.0.2-alpha.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=CodeCorrectCollective.Blocks.EntityFramework.Testing&version=1.0.2-alpha.0.1&prerelease
                    
Install as a Cake Addin
#tool nuget:?package=CodeCorrectCollective.Blocks.EntityFramework.Testing&version=1.0.2-alpha.0.1&prerelease
                    
Install as a Cake Tool

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.

Open in DevPod!

Connecting to a custom database engine

  1. 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;
     }
    
  2. Modify the above code to create your own DbConnection and to return the correct DbConnectionOptions<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

  1. The test class should derive from EntityFrameworkSqliteTest<TDBContext> where TDbContext is the Dbcontext type specific to your project.
  2. 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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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