-
-
Notifications
You must be signed in to change notification settings - Fork 545
Expand file tree
/
Copy pathEfCorePostgresContainerFixture.cs
More file actions
31 lines (22 loc) · 997 Bytes
/
EfCorePostgresContainerFixture.cs
File metadata and controls
31 lines (22 loc) · 997 Bytes
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
using Microsoft.EntityFrameworkCore;
using Core.Testing.Fixtures;
using Xunit;
namespace Core.EntityFramework.Tests.Fixtures;
public class EfCorePostgresContainerFixture<TContext>: IAsyncLifetime where TContext : DbContext
{
private readonly PostgresContainerFixture postgresContainerFixture = new();
public TContext DbContext { get; private set; } = null!;
public async Task InitializeAsync()
{
await postgresContainerFixture.InitializeAsync().ConfigureAwait(false);
var optionsBuilder = new DbContextOptionsBuilder<TContext>()
.UseNpgsql(postgresContainerFixture.DataSource);
DbContext = (TContext)Activator.CreateInstance(typeof(TContext), optionsBuilder.Options)!;
await DbContext.Database.MigrateAsync().ConfigureAwait(false);
}
public async Task DisposeAsync()
{
await DbContext.DisposeAsync().ConfigureAwait(false);
await postgresContainerFixture.DisposeAsync().ConfigureAwait(false);
}
}