Files
zszq-trs/UnitTestProject/Modules/SwapModule/TestableSwapEodPositionService.cs
T
hjhan 7b007bddfa refactor(swap-test): 抽取 TestableSwapEodPositionService 公共基类收敛重复 Stub
- 新建 TestableSwapEodPositionService 收敛 8/8 Stub 重复的高频 override
  (PersistEodSwapPosition/SaveAllChanges/GetCurrencyRate/AddClientCash)
  + 统一 OptUserInfo 构造 + PersistedPositions/SaveChangesCount 输出捕获
- SwapEodPositionService.DealInterests 改 protected virtual(行为零变化)
- 8 个 ScenarioTest 改为继承基类,删除重复 override
- 消灭 DealInterestsScenarioTest/DealInterestsGoldenReplayTest 的反射调用
  (typeof().GetMethod().Invoke → 直接调用 DealInterests)

验证:dotnet build 0 错误;dotnet test SwapModule 284通过/6跳过/0失败
2026-07-23 11:16:11 +08:00

69 lines
2.9 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 YLErp.DBModels;
using YLErp.DBModels.Enums;
using YLErp.Model;
namespace YLErp.Modules.SwapModule
{
/// <summary>
/// SwapEodPositionService 的可测试化基类(纯内存,不连库)。
/// ============================================================================
/// 收敛各 ScenarioTest 中 Stub 子类的重复 override
/// - PersistEodSwapPosition:收集到列表,不写库
/// - SaveAllChangesno-op
/// - GetCurrencyRate:返回 1.0(本币)
/// - 统一构造函数(注入 OptUserInfo,标记 UnitTest 来源)
///
/// 暴露 PersistedPositions / SaveChangesCount / ClientCashCalls 等输出捕获属性,
/// 供断言使用。各测试子类按需再 override 业务 seamFindTrade/GetUnderlyingPrice 等)。
///
/// 设计原则:
/// - 只收敛 8/8 Stub 都重复的高频 override,不预设业务数据注入方式
/// ComposePage 用属性字典、SwapPositionCompose 用构造函数 List,差异留给子类)
/// - 不提供 Execute* 包装器(签名各异且大多只出现 1-2 次,留在各子类避免基类膨胀)
/// ============================================================================
/// </summary>
public class TestableSwapEodPositionService : SwapEodPositionService
{
/// <summary>捕获所有持久化的 eod 持仓(按调用顺序)</summary>
public List<eod_swap_position> PersistedPositions { get; } = new();
/// <summary>SaveAllChanges 调用次数</summary>
public int SaveChangesCount { get; private set; }
/// <summary>AddClientCash 调用记录(金额, 操作)</summary>
public List<(double amount, string action)> ClientCashCalls { get; } = new();
/// <summary>自增 id 模拟器(新增 eod 时分配 id</summary>
private int _nextId = 1;
protected TestableSwapEodPositionService(string testName)
: base(new OptUserInfo(0, testName ?? nameof(TestableSwapEodPositionService), OptUserFrom.UnitTest))
{
}
// ===== 高频 seam override8/8 Stub 都重复,收敛到基类)=====
protected override void PersistEodSwapPosition(eod_swap_position position)
{
if (position.id == 0) position.id = _nextId++;
PersistedPositions.Add(position);
}
protected override void SaveAllChanges()
{
SaveChangesCount++;
}
protected override double GetCurrencyRate(string quoteCurrency, string settlementCurrency, DateTime valueDate, bool seekPreday, CurrencyRateType currencyRateType)
{
return 1.0; // 本币,汇率=1
}
protected override int AddClientCash(trade td, double amount, string action, DateTime valueDate)
{
ClientCashCalls.Add((amount, action));
return _nextId++;
}
}
}