Files
manga-reader/MangaReader.Tests/Utilities/TestDbContextFactory.cs

36 lines
933 B
C#

using MangaReader.Core.Data;
using Microsoft.Data.Sqlite;
using Microsoft.EntityFrameworkCore;
namespace MangaReader.Tests.Utilities;
public class TestDbContextFactory : IDisposable
{
private readonly SqliteConnection _connection;
private readonly DbContextOptions<MangaContext> _options;
public TestDbContextFactory()
{
_connection = new SqliteConnection("DataSource=:memory:");
_connection.Open();
_options = new DbContextOptionsBuilder<MangaContext>()
.UseSqlite(_connection)
.EnableSensitiveDataLogging() // Optional: helps with debugging
.Options;
using MangaContext context = new(_options);
context.Database.EnsureCreated();
}
public MangaContext CreateContext()
{
return new MangaContext(_options);
}
public void Dispose()
{
_connection.Close();
_connection.Dispose();
}
}