-
Notifications
You must be signed in to change notification settings - Fork 2
/
RegisteredDbContextFactoryTests.cs
75 lines (62 loc) · 2.75 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
using FluentAssertions;
using Microsoft.EntityFrameworkCore;
using System;
using Xunit;
using Zejji.Entity;
using Zejji.Tests.Helpers;
using Zejji.Tests.Models;
namespace Zejji.Tests
{
public sealed class RegisteredDbContextFactoryTests : IDisposable
{
private readonly SqliteMemoryDatabaseLifetimeManager _databaseManager;
private readonly RegisteredDbContextFactory _dbContextFactory;
public RegisteredDbContextFactoryTests()
{
// Create a SQLite in-memory database which will last the duration of the test
_databaseManager = new SqliteMemoryDatabaseLifetimeManager();
_dbContextFactory = new RegisteredDbContextFactory();
}
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);
}
}
}