work-flow/tests/Workflow.Tests/Form/FormTestFixture.cs
向宁 fc4ecbbacc feat: add gRPC auth, condition comparators, seed data, EF migrations
- gRPC auth service for token validation
- Value comparator system (string, numeric, boolean, datetime, collection)
- Condition evaluator with strategy chain
- Form definition and data improvements
- Workflow instance/task endpoints updated
- Seed data and EF design-time factory
- Test coverage for comparators and handlers
2026-05-20 20:28:35 +08:00

60 lines
1.8 KiB
C#
Raw Permalink 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;
using Workflow.Infrastructure.Persistence;
using Xunit;
namespace Workflow.Tests.Form;
/// <summary>
/// 表单模块测试夹具,提供 InMemory 数据库上下文及通用辅助方法。
/// 每个测试方法应使用独立的 database name 以确保数据隔离。
/// </summary>
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;
return new WorkflowDbContext(options);
}
/// <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;
/// <summary>
/// xUnit CollectionFixture用于跨测试类共享 Fixture 实例。
/// </summary>
[CollectionDefinition("FormTests")]
public class FormTestCollection : ICollectionFixture<FormTestFixtureClassFixture>;