51 lines
1.4 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Microsoft.EntityFrameworkCore;
namespace BuildFlow.Workflow.Tests.Form;
public class FormTestFixture : IDisposable
{
/// <summary>
/// 创建一个独立的 InMemory WorkflowDbContext 实例。
/// 每次调用都使用新的 database name,确保测试之间互不干扰。
/// </summary>
public WorkflowDbContext CreateDbContext([System.Runtime.CompilerServices.CallerMemberName] string testName = "")
{
var options = new DbContextOptionsBuilder<WorkflowDbContext>()
.UseInMemoryDatabase(databaseName: $"WorkflowTest_{testName}_{Guid.NewGuid()}")
.Options;
var db = new WorkflowDbContext(options);
db.SavingChanges += AuditStub.OnSave;
return db;
}
/// <summary>
/// 创建一个已包含种子表单定义的 DbContext,用于需要已有数据的测试场景。
/// </summary>
public async Task<WorkflowDbContext> CreateDbContextWithSeedAsync(
string testName,
Action<WorkflowDbContext>? seedAction = null)
{
var db = CreateDbContext(testName);
if (seedAction is not null)
{
seedAction(db);
await db.SaveChangesAsync();
}
return db;
}
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
/// <summary>
/// xUnit ClassFixture,用于在测试类级别共享 Fixture 实例。
/// </summary>
public class FormTestFixtureClassFixture : FormTestFixture;