-
-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathBaseEfRepoTestFixture.cs
47 lines (39 loc) · 1.72 KB
/
BaseEfRepoTestFixture.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
using CleanArchitecture.Application.Interfaces;
using CleanArchitecture.Infrastructure.Persistence.Contexts;
using CleanArchitecture.Infrastructure.Persistence.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace CleanArchitecture.IntegrationTests.Data;
public abstract class BaseEfRepoTestFixture
{
protected ApplicationDbContext dbContext;
protected BaseEfRepoTestFixture()
{
var options = CreateNewContextOptions();
IAuthenticatedUserService authenticatedUserService = new AuthenticatedUserService(Guid.NewGuid().ToString(), "UserName");
dbContext = new ApplicationDbContext(options, authenticatedUserService);
}
protected static DbContextOptions<ApplicationDbContext> CreateNewContextOptions()
{
// Create a fresh service provider, and therefore a fresh
// InMemory database instance.
var serviceProvider = new ServiceCollection()
.AddEntityFrameworkInMemoryDatabase()
.BuildServiceProvider();
// Create a new options instance telling the context to use an
// InMemory database and the new service provider.
var builder = new DbContextOptionsBuilder<ApplicationDbContext>();
builder.UseInMemoryDatabase(nameof(ApplicationDbContext))
.UseInternalServiceProvider(serviceProvider);
return builder.Options;
}
protected GenericRepository<T> GetRepository<T>() where T : class
{
return new GenericRepository<T>(dbContext);
}
protected IUnitOfWork GetUnitOfWork()
{
return new UnitOfWork(dbContext);
}
}
internal record AuthenticatedUserService(string UserId, string UserName) : IAuthenticatedUserService;