SqliteMemoryDatabaseProvider.AutoMocker 7.2.3

dotnet add package SqliteMemoryDatabaseProvider.AutoMocker --version 7.2.3
NuGet\Install-Package SqliteMemoryDatabaseProvider.AutoMocker -Version 7.2.3
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="SqliteMemoryDatabaseProvider.AutoMocker" Version="7.2.3" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add SqliteMemoryDatabaseProvider.AutoMocker --version 7.2.3
#r "nuget: SqliteMemoryDatabaseProvider.AutoMocker, 7.2.3"
#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 SqliteMemoryDatabaseProvider.AutoMocker as a Cake Addin
#addin nuget:?package=SqliteMemoryDatabaseProvider.AutoMocker&version=7.2.3

// Install SqliteMemoryDatabaseProvider.AutoMocker as a Cake Tool
#tool nuget:?package=SqliteMemoryDatabaseProvider.AutoMocker&version=7.2.3

SqliteMemoryDatabaseProvider.AutoMocker

This package provides overloads for AutoMocker and DbContext to aid with creating in-memory databases for unit tests.

Create Database

CreateInMemoryDatabase is an overload for AutoMocker that allows you to create a database in-memory using SQLite and tell the mock on the test to use it.

Optionally, you can provide actions to execute after the database is created. Commonly, this can be used to populate data into your new database. These changes are saved automatically.

An example of this in a test:

var mock = new AutoMocker();
mock.CreateInMemoryDatabase<ITestEntities, TestEntities>(x =>
    x.TestModels.Add(testRecord)
);
var testClass = mock.CreateInstance<TestClass>();

In the above example, ITestEntities is injected into TestClass. When CreateInMemoryDatabase is called, a new TestEntities is created in memory, the testRecord is inserted, and then mock is told to use the new TestEntities as an implementation for ITestEntities.

This method also returns the database if you want to modify or get data from it after it's created.

var database = mock.CreateInMemoryDatabase<ITestEntities, TestEntities>();
database.TestModels.Add(testRecord);
database.SaveChanges();
var testClass = mock.CreateInstance<TestClass>();
var records = database.TestModels.ToList();
database.CloseInMemoryDatabaseConnection();

Additional Parameters

If your entity needs additional parameters, an overload is available to provide them:

var mock = new AutoMocker();
var mockedDependency = mock.GetMock<ITestDependencies>();
mock.CreateInMemoryDatabase<ITestEntities, TestEntities>(mockedDependency.Object);
var testClass = mock.CreateInstance<TestClass>();

You can optionally combine providing actions and additional constructor parameters.

var mock = new AutoMocker();
var mockedDependency = mock.GetMock<ITestDependencies>();
mock.CreateInMemoryDatabase<ITestEntities, TestEntities>(x => {
    x.TestModels.Add(testRecord)
}, mockedDependency.Object);
var testClass = mock.CreateInstance<TestClass>();

SQLite Type Converters

By default, SQLite doesn't support certain types, such as DateTimeOffset, Decimal, and TimeSpan. This package automatically sets up converters by these when setting up the SQLite database. You can choose to not utilize these converters by using an overload:

var mock = new AutoMocker();
var mockDatabase = mock.CreateInMemoryDatabase<TestEntities>(false, x =>
    x.TestModels.Add(testRecord);
);

Closing In-Memory Database Connections

Although tests are short lived, it's still best practice to close database connections upon test completion.

Two extension methods are provided to close in-memory database connections for your DbContext.

CloseInMemoryDatabaseConnection will close a singular connection from the generated database.

var mock = new AutoMocker();
var database = mock.CreateInMemoryDatabase<ITestEntities, TestEntities>();
database.CloseInMemoryDatabaseConnection();

CloseInMemoryDatabaseConnections will close one or many database connections from the inputted DbContext instances. This can be useful if multiple databases were set up in your tests.

var mock = new AutoMocker();
var database1 = mock.CreateInMemoryDatabase<ITestEntities1, TestEntities1>();
var database2 = mock.CreateInMemoryDatabase<ITestEntities2, TestEntities2>();
DbContextExtensions.CloseInMemoryDatabaseConnections(database1, database2);

Contribute

If you encounter an issue or want to contribute to this package, please visit this package's GitHub page.

Product Compatible and additional computed target framework versions.
.NET 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 is compatible.  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 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.

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
7.2.3 92 2/15/2024
7.2.2 78 2/15/2024
7.2.1 168 11/15/2023
7.2.0 101 11/15/2023
7.1.0 147 7/30/2023
7.0.0 275 1/26/2023
6.0.0 274 1/25/2023
1.0.3 272 1/9/2023
1.0.2 297 1/5/2023
1.0.1 296 1/3/2023
1.0.0 283 1/3/2023