SqliteMemoryDatabaseProvider.AutoMocker
7.1.0
See the version list below for details.
dotnet add package SqliteMemoryDatabaseProvider.AutoMocker --version 7.1.0
NuGet\Install-Package SqliteMemoryDatabaseProvider.AutoMocker -Version 7.1.0
<PackageReference Include="SqliteMemoryDatabaseProvider.AutoMocker" Version="7.1.0" />
paket add SqliteMemoryDatabaseProvider.AutoMocker --version 7.1.0
#r "nuget: SqliteMemoryDatabaseProvider.AutoMocker, 7.1.0"
// Install SqliteMemoryDatabaseProvider.AutoMocker as a Cake Addin #addin nuget:?package=SqliteMemoryDatabaseProvider.AutoMocker&version=7.1.0 // Install SqliteMemoryDatabaseProvider.AutoMocker as a Cake Tool #tool nuget:?package=SqliteMemoryDatabaseProvider.AutoMocker&version=7.1.0
SqliteMemoryDatabaseProvider.AutoMocker
This package provides overloads for AutoMocker
and DbContext
to aid with creating in-memory databases for unit tests. This package should only be used with projects using EntityFramework version 7.x.x.
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 | Versions 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 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. |
-
net6.0
- Moq.AutoMock (>= 3.5.0)
- SqliteMemoryDatabaseProvider (>= 7.1.0)
-
net7.0
- Moq.AutoMock (>= 3.5.0)
- SqliteMemoryDatabaseProvider (>= 7.1.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.