Objective and Problem
I want to automatically both create a separate database file for each integration test case and delete it right after the test.
I don’t understand the culprit why the creation success but the deletion fails. Could you figure out the culprit?
The code below is intentionally made simpler only for illustrating the issue.
Web API Project
A globally unique database file name is create for each test case.
<code>using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlite($"DataSource={Guid.NewGuid()}.db"));
var app = builder.Build();
app.Run();
public record Person(int Id);
public class AppDbContext(DbContextOptions<AppDbContext> o) : DbContext(o)
{
public DbSet<Person> People { get; set; }
}
public interface IWebApiMarker;
</code>
<code>using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlite($"DataSource={Guid.NewGuid()}.db"));
var app = builder.Build();
app.Run();
public record Person(int Id);
public class AppDbContext(DbContextOptions<AppDbContext> o) : DbContext(o)
{
public DbSet<Person> People { get; set; }
}
public interface IWebApiMarker;
</code>
using Microsoft.EntityFrameworkCore;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddDbContext<AppDbContext>(o => o.UseSqlite($"DataSource={Guid.NewGuid()}.db"));
var app = builder.Build();
app.Run();
public record Person(int Id);
public class AppDbContext(DbContextOptions<AppDbContext> o) : DbContext(o)
{
public DbSet<Person> People { get; set; }
}
public interface IWebApiMarker;
Test Project
BaseTest
creates database file and its DisposeAsync()
deletes the file.
<code>using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
namespace WebApi.Test;
public class Test_1 : BaseTest
{
[Fact] public async Task Test() => await Task.Delay(2000);
}
public abstract class BaseTest : IAsyncDisposable
{
private readonly WebApplicationFactory<IWebApiMarker> _factory;
protected BaseTest()
{
_factory = new WebApplicationFactory<IWebApiMarker>();
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureCreated();
}
public async ValueTask DisposeAsync()
{
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureDeleted();
await _factory.DisposeAsync();
}
}
</code>
<code>using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
namespace WebApi.Test;
public class Test_1 : BaseTest
{
[Fact] public async Task Test() => await Task.Delay(2000);
}
public abstract class BaseTest : IAsyncDisposable
{
private readonly WebApplicationFactory<IWebApiMarker> _factory;
protected BaseTest()
{
_factory = new WebApplicationFactory<IWebApiMarker>();
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureCreated();
}
public async ValueTask DisposeAsync()
{
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureDeleted();
await _factory.DisposeAsync();
}
}
</code>
using Microsoft.AspNetCore.Mvc.Testing;
using Microsoft.Extensions.DependencyInjection;
namespace WebApi.Test;
public class Test_1 : BaseTest
{
[Fact] public async Task Test() => await Task.Delay(2000);
}
public abstract class BaseTest : IAsyncDisposable
{
private readonly WebApplicationFactory<IWebApiMarker> _factory;
protected BaseTest()
{
_factory = new WebApplicationFactory<IWebApiMarker>();
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureCreated();
}
public async ValueTask DisposeAsync()
{
using var scope = _factory.Services.CreateScope();
var db = scope.ServiceProvider.GetRequiredService<AppDbContext>();
db.Database.EnsureDeleted();
await _factory.DisposeAsync();
}
}