-
Notifications
You must be signed in to change notification settings - Fork 2
/
RegisteredDbContextFactoryTests.cs
57 lines (46 loc) · 2.34 KB
/
RegisteredDbContextFactoryTests.cs
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using Slant.Entity.Tests.Helpers;
using Slant.Entity.Tests.Models;
using System;
using Xunit;
namespace Slant.Entity.Tests
{
public sealed class RegisteredDbContextFactoryTests : IDisposable
{
// Create a SQLite in-memory database which will last the duration of the test
private readonly SqliteMemoryDatabaseLifetimeManager _databaseManager = new();
private readonly RegisteredDbContextFactory _dbContextFactory = new();
public void Dispose()
{
_databaseManager.Dispose();
}
[Fact]
public void RegisteredDbContextFactory_should_call_registered_factory_function_for_type()
{
var connectionString = _databaseManager.ConnectionString;
var testDbContextFactoryCallCount = 0;
var emptyDbContextFactoryCallCount = 0;
// Arrange - register factory functions for the TestDbContext and EmptyDbContext types
_dbContextFactory.RegisterDbContextType<TestDbContext>(() =>
{ testDbContextFactoryCallCount += 1; return new TestDbContext(connectionString); });
_dbContextFactory.RegisterDbContextType<EmptyDbContext>(() =>
{ emptyDbContextFactoryCallCount += 1; return new EmptyDbContext(); });
// Act - ask the factory for some DbContexts
var testDbContext1 = _dbContextFactory.CreateDbContext<TestDbContext>();
var testDbContext2 = _dbContextFactory.CreateDbContext<TestDbContext>();
var emptyDbContext = _dbContextFactory.CreateDbContext<EmptyDbContext>();
// Assert
testDbContextFactoryCallCount.Should().Be(2);
emptyDbContextFactoryCallCount.Should().Be(1);
testDbContext1.Should().NotBeNull();
testDbContext2.Should().NotBeNull();
emptyDbContext.Should().NotBeNull();
testDbContext1.Should().NotBeSameAs(testDbContext2);
var testDbContext1ConnectionString = testDbContext1.Database.GetDbConnection().ConnectionString;
testDbContext1ConnectionString.Should().Be(connectionString);
var testDbContext2ConnectionString = testDbContext2.Database.GetDbConnection().ConnectionString;
testDbContext2ConnectionString.Should().Be(connectionString);
}
}
}