-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathDatabaseFactsBase.cs
More file actions
38 lines (30 loc) · 1.02 KB
/
DatabaseFactsBase.cs
File metadata and controls
38 lines (30 loc) · 1.02 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace AddressBook;
/// <summary>
/// Instantiates a test <typeparamref name="TSubject"/>, injecting an in-memory database and mocks for its other dependencies.
/// </summary>
public class DatabaseFactsBase<TSubject> : AutoMockingFactsBase<TSubject>
where TSubject : class
{
private readonly SqliteConnection _connection;
/// <summary>
/// An in-memory database that is reset after every test.
/// </summary>
protected readonly AddressBookDbContext Context;
protected DatabaseFactsBase()
{
_connection = new SqliteConnection("Data Source=:memory:");
_connection.Open();
Context = new AddressBookDbContext(
new DbContextOptionsBuilder().UseSqlite(_connection).EnableSensitiveDataLogging().Options);
Context.Database.EnsureCreated();
Use(Context);
}
public override void Dispose()
{
Context.Dispose();
_connection.Dispose();
base.Dispose();
}
}