SqliteMemoryDatabaseProvider 1.0.0
See the version list below for details.
dotnet add package SqliteMemoryDatabaseProvider --version 1.0.0
NuGet\Install-Package SqliteMemoryDatabaseProvider -Version 1.0.0
<PackageReference Include="SqliteMemoryDatabaseProvider" Version="1.0.0" />
paket add SqliteMemoryDatabaseProvider --version 1.0.0
#r "nuget: SqliteMemoryDatabaseProvider, 1.0.0"
// Install SqliteMemoryDatabaseProvider as a Cake Addin #addin nuget:?package=SqliteMemoryDatabaseProvider&version=1.0.0 // Install SqliteMemoryDatabaseProvider as a Cake Tool #tool nuget:?package=SqliteMemoryDatabaseProvider&version=1.0.0
SqliteMemoryDatabaseProvider
This provider to aid with creating in-memory databases for unit tests.
Create Database
You can set up a new SqliteMemoryDatabaseProvider
and call CreateDatabase
. This will set up the database in-memory to be used in your test. SqliteMemoryDatabaseProvider
needs to be instantiated so it can open up a new database connection using in-memory SQLite.
An example of this in a test:
var mock = new AutoMocker();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>();
mockDatabase.TestModels.Add(testRecord);
await mockDatabase.SaveChangesAsync();
mock.Use<ITestEntities>(mockDatabase);
var testClass = mock.CreateInstance<TestClass>();
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();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(x =>
x.TestModels.Add(testRecord);
);
mock.Use<ITestEntities>(mockDatabase);
var testClass = mock.CreateInstance<TestClass>();
SqliteMemoryDatabaseProvider
can also be used at a class level for your tests:
private SqliteMemoryDatabaseProvider sqliteMemoryDatabaseProvider;
[SetUp]
public void SetUp()
{
sqliteMemoryDatabaseProvider = new SqliteMemoryDatabaseProvider();
}
[TearDown]
public void TearDown()
{
sqliteMemoryDatabaseProvider.Dispose();
}
This can help simplify your tests like so:
var mock = new AutoMocker();
var mockDatabase = sqliteMemoryDatabaseProvider.CreateDatabase<TestEntities>(x =>
x.TestModels.Add(testRecord);
);
mock.Use<ITestEntities>(mockDatabase);
var testClass = mock.CreateInstance<TestClass>();
Additional Parameters
If your entity needs additional parameters, an overload is available to provide them:
var mock = new AutoMocker();
var mockedDependency = mock.GetMock<ITestDependencies>();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(mockedDependency.Object);
await mockDatabase.SaveChangesAsync();
var testClass = mock.CreateInstance<TestClass>();
You can optionally combine providing actions and additional constructor parameters.
var mock = new AutoMocker();
var mockedDependency = mock.GetMock<ITestDependencies>();
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<TestEntities>(x => {
x.TestModels.Add(testRecord);
}, mockedDependency.Object);
await mockDatabase.SaveChangesAsync();
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:
using var provider = new SqliteMemoryDatabaseProvider();
var mockDatabase = provider.CreateDatabase<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.
SqliteMemoryDatabaseProvider
will automatically close its database connections when it's being disposed. The simplest ways to do this are to either set up your provider at the class level or scope the provider in your test to dispose when done by using the using
keyword:
Scope Provider
using var provider = new SqliteMemoryDatabaseProvider();
When the test completes, the database connection will automatically be closed and disposed.
Class Level
You can also set up the SqliteMemoryDatabaseProvider
at the class level and call its Dispose
method during test teardown:
private SqliteMemoryDatabaseProvider sqliteMemoryDatabaseProvider;
[SetUp]
public void SetUp()
{
sqliteMemoryDatabaseProvider = new SqliteMemoryDatabaseProvider();
}
[TearDown]
public void TearDown()
{
sqliteMemoryDatabaseProvider.Dispose();
}
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 was computed. 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
- Microsoft.EntityFrameworkCore.Sqlite (>= 6.0.12)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on SqliteMemoryDatabaseProvider:
Package | Downloads |
---|---|
SqliteMemoryDatabaseProvider.AutoMocker
For creating in-memory SQLite databases to be used within unit tests using AutoMocker. |
GitHub repositories
This package is not used by any popular GitHub repositories.
1.0.0 - Created SqliteMemoryDatabaseProvider.