c# - 在集成测试之间重置 In-Memory 数据库
问题描述
我已经建立了一个基于https://github.com/jasontaylordev/CleanArchitecture的项目。但是我在为控制器编写集成测试时遇到了一些麻烦,因为内存数据库不会在每次测试之间重置。每个测试都使用 WebApplicationFactory 来设置测试网络服务器,
public class CustomWebApplicationFactory<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(services =>
{
// Remove the app's ApplicationDbContext registration.
var descriptor = services.SingleOrDefault(
d => d.ServiceType ==
typeof(DbContextOptions<ApplicationDbContext>));
if (descriptor != null)
{
services.Remove(descriptor);
}
// Add a database context using an in-memory
// database for testing.
services.AddDbContext<ApplicationDbContext>(options =>
{
options.UseInMemoryDatabase("InMemoryDbForTesting");
});
// Register test services
services.AddScoped<ICurrentUserService, TestCurrentUserService>();
services.AddScoped<IDateTime, TestDateTimeService>();
services.AddScoped<IIdentityService, TestIdentityService>();
// Build the service provider
var sp = services.BuildServiceProvider();
// Create a scope to obtain a reference to the database
// context (ApplicationDbContext).
using (var scope = sp.CreateScope())
{
var scopedServices = scope.ServiceProvider;
var context = scopedServices.GetRequiredService<ApplicationDbContext>();
var logger = scopedServices.GetRequiredService<ILogger<CustomWebApplicationFactory<TStartup>>>();
// Ensure the database is created.
context.Database.EnsureCreated();
try
{
// Seed the database with test data.
SeedSampleData(context);
}
catch (Exception ex)
{
logger.LogError(ex, "An error occurred seeding the database with test messages. Error: {Message}", ex.Message);
}
}
})
.UseEnvironment("Test");
}
...
以下创建测试:
namespace CleanArchitecture.WebUI.IntegrationTests.Controllers.TodoItems
{
public class Create : IClassFixture<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _factory;
public Create(CustomWebApplicationFactory<Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task GivenValidCreateTodoItemCommand_ReturnsSuccessCode()
{
var client = await _factory.GetAuthenticatedClientAsync();
var command = new CreateTodoItemCommand
{
Title = "Do yet another thing."
};
var content = IntegrationTestHelper.GetRequestContent(command);
var response = await client.PostAsync($"/api/todoitems", content);
response.EnsureSuccessStatusCode();
}
...
以及以下阅读测试:
namespace CleanArchitecture.WebUI.IntegrationTests.Controllers.TodoItems
{
public class Read: IClassFixture<CustomWebApplicationFactory<Startup>>
{
private readonly CustomWebApplicationFactory<Startup> _factory;
public Read(CustomWebApplicationFactory<Startup> factory)
{
_factory = factory;
}
[Fact]
public async Task ShouldRetriveAllTodos()
{
var client = await _factory.GetAuthenticatedClientAsync();
var response = await client.GetAsync($"/api/todoitems");
var todos = Deserialize(response);
todos.Count().Should().Be(1); //but is 2 because database is shared between read and create test class.
}
问题是内存数据库在每次测试之间没有重置。我尝试使用 为内存数据库生成不同的名称Guid.New.ToString
,但是测试找不到种子数据库数据,并且将测试放在同一个 XUnit 集合中,但无济于事。
有什么好主意如何使测试不共享数据库?
解决方案
对我有用的是为每个WebApplicationFactory
实例生成一个 DBName,并为每个测试实例化其中一个。所以测试看起来像这样:
[Fact]
public void Test()
{
// Arrange
var appFactory = new WebApplicationFactory();
// ...
// Act
// ...
// Assert
// ...
}
和WebApplicationFactory
:
public class TestWebApplicationFactory : WebApplicationFactory<Startup>
{
private readonly string _dbName = Guid.NewGuid().ToString();
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
builder.ConfigureServices(services =>
{
services.AddDbContext<DbContext>(options =>
{
// This is what makes a unique in-memory database per instance of TestWebApplicationFactory
options.UseInMemoryDatabase(_dbName);
});
});
}
}
推荐阅读
- flutter - 如何在gridview中随机设置iconbutton图标?
- python - 在 python 中绘制多个过滤数据集
- robotics - 从远程定制 PC 读取 Jetson Nano 相机的实时数据
- php - AWS 代码管道与 sonarcloud 的集成
- c++ - 使用 MatrixXd 进行本征求解器计算并抛出错误
- java - 如何在 android 中使用 Gson 反序列化 LocalDateTime?
- python - 使用广播对 2D 和 3D 数组求和
- python - 为什么我在使用 pytube 时会收到这些错误?
- python - 带方括号的正则表达式
- javascript - 从下拉列表中选择的数据不会出现在警报消息中