From a7be41367a1a9de80b5d4506f4d2dd63c65ca80d Mon Sep 17 00:00:00 2001 From: gewuyou <95328647+GeWuYou@users.noreply.github.com> Date: Tue, 28 Apr 2026 07:48:55 +0800 Subject: [PATCH] =?UTF-8?q?fix(core-tests):=20=E6=B6=88=E9=99=A4=20GameCon?= =?UTF-8?q?textTests=20=E8=AD=A6=E5=91=8A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 拆分 TestArchitecture 与 TestArchitectureContext 到独立测试文件以消除 MA0048 - 修复 TestArchitectureContext 的可空签名以匹配 IArchitectureContext 契约 - 补充测试桩公开成员的 XML 文档并保持 GameContextTests 行为不变 --- .../Architectures/GameContextTests.cs | 396 ---------------- .../Architectures/TestArchitecture.cs | 16 + .../Architectures/TestArchitectureContext.cs | 427 ++++++++++++++++++ 3 files changed, 443 insertions(+), 396 deletions(-) create mode 100644 GFramework.Core.Tests/Architectures/TestArchitecture.cs create mode 100644 GFramework.Core.Tests/Architectures/TestArchitectureContext.cs diff --git a/GFramework.Core.Tests/Architectures/GameContextTests.cs b/GFramework.Core.Tests/Architectures/GameContextTests.cs index fd6e2bbb..0087102a 100644 --- a/GFramework.Core.Tests/Architectures/GameContextTests.cs +++ b/GFramework.Core.Tests/Architectures/GameContextTests.cs @@ -1,20 +1,4 @@ -using GFramework.Core.Abstractions.Architectures; -using GFramework.Core.Abstractions.Command; -using GFramework.Core.Abstractions.Environment; -using GFramework.Core.Abstractions.Events; -using GFramework.Core.Abstractions.Ioc; -using GFramework.Core.Abstractions.Model; -using GFramework.Core.Abstractions.Query; -using GFramework.Core.Abstractions.Systems; -using GFramework.Core.Abstractions.Utility; using GFramework.Core.Architectures; -using GFramework.Core.Command; -using GFramework.Core.Environment; -using GFramework.Core.Events; -using GFramework.Core.Ioc; -using GFramework.Core.Query; -using GFramework.Cqrs.Abstractions.Cqrs; -using ICommand = GFramework.Core.Abstractions.Command.ICommand; namespace GFramework.Core.Tests.Architectures; @@ -211,383 +195,3 @@ public class GameContextTests Assert.That(GameContext.ArchitectureReadOnlyDictionary.Count, Is.EqualTo(0)); } } - -/// -/// 测试用的架构类,继承自Architecture -/// -public class TestArchitecture : Architecture -{ - /// - /// 初始化方法,当前为空实现 - /// - protected override void OnInitialize() - { - } -} - -/// -/// 测试用的架构上下文类,实现了IArchitectureContext接口 -/// -public class TestArchitectureContext : IArchitectureContext -{ - private readonly MicrosoftDiContainer _container = new(); - - /// - /// 获取依赖注入容器 - /// - public IIocContainer Container => _container; - - /// - /// 获取事件总线 - /// - public IEventBus EventBus => new EventBus(); - - /// - /// 获取命令总线 - /// - public ICommandExecutor CommandExecutor => new CommandExecutor(); - - /// - /// 获取查询总线 - /// - public IQueryExecutor QueryExecutor => new QueryExecutor(); - - /// - /// 获取环境对象 - /// - public IEnvironment Environment => new DefaultEnvironment(); - - /// - /// 获取指定类型的服务 - /// - /// 服务类型 - /// 服务实例或null - public TService? GetService() where TService : class - { - return _container.Get(); - } - - /// - /// 获取指定类型的所有服务 - /// - /// 服务类型 - /// 所有服务实例列表 - public IReadOnlyList GetServices() where TService : class - { - return _container.GetAll(); - } - - /// - /// 获取指定类型的模型 - /// - /// 模型类型 - /// 模型实例或null - public TModel? GetModel() where TModel : class, IModel - { - return _container.Get(); - } - - /// - /// 获取指定类型的所有模型 - /// - /// 模型类型 - /// 所有模型实例列表 - public IReadOnlyList GetModels() where TModel : class, IModel - { - return _container.GetAll(); - } - - /// - /// 获取指定类型的系统 - /// - /// 系统类型 - /// 系统实例或null - public TSystem? GetSystem() where TSystem : class, ISystem - { - return _container.Get(); - } - - /// - /// 获取指定类型的所有系统 - /// - /// 系统类型 - /// 所有系统实例列表 - public IReadOnlyList GetSystems() where TSystem : class, ISystem - { - return _container.GetAll(); - } - - /// - /// 获取指定类型的工具 - /// - /// 工具类型 - /// 工具实例或null - public virtual TUtility? GetUtility() where TUtility : class, IUtility - { - return _container.Get(); - } - - /// - /// 获取指定类型的所有工具 - /// - /// 工具类型 - /// 所有工具实例列表 - public IReadOnlyList GetUtilities() where TUtility : class, IUtility - { - return _container.GetAll(); - } - - public IReadOnlyList GetServicesByPriority() where TService : class - { - return _container.GetAllByPriority(); - } - - public IReadOnlyList GetSystemsByPriority() where TSystem : class, ISystem - { - return _container.GetAllByPriority(); - } - - public IReadOnlyList GetModelsByPriority() where TModel : class, IModel - { - return _container.GetAllByPriority(); - } - - public IReadOnlyList GetUtilitiesByPriority() where TUtility : class, IUtility - { - return _container.GetAllByPriority(); - } - - /// - /// 发送事件 - /// - /// 事件类型 - public void SendEvent() where TEvent : new() - { - } - - /// - /// 发送事件 - /// - /// 事件类型 - /// 事件实例 - public void SendEvent(TEvent e) where TEvent : class - { - } - - /// - /// 注册事件处理器 - /// - /// 事件类型 - /// 事件处理委托 - /// 取消注册接口 - public IUnRegister RegisterEvent(Action handler) - { - return new DefaultUnRegister(() => { }); - } - - /// - /// 取消注册事件处理器 - /// - /// 事件类型 - /// 事件处理委托 - public void UnRegisterEvent(Action onEvent) - { - } - - /// - /// 测试桩:异步发送统一 CQRS 请求。 - /// - /// 响应类型。 - /// 要发送的请求。 - /// 取消令牌。 - /// 请求响应任务。 - /// 该测试桩不支持此成员。 - public ValueTask SendRequestAsync(IRequest request, - CancellationToken cancellationToken = default) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:同步发送统一 CQRS 请求。 - /// - /// 响应类型。 - /// 要发送的请求。 - /// 请求响应。 - /// 该测试桩不支持此成员。 - public TResponse SendRequest(IRequest request) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:异步发送 CQRS 命令并返回响应。 - /// - /// 命令响应类型。 - /// 要发送的命令。 - /// 取消令牌。 - /// 命令响应任务。 - /// 该测试桩不支持此成员。 - public ValueTask SendCommandAsync( - GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command, - CancellationToken cancellationToken = default) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:同步发送 CQRS 命令并返回响应。 - /// - /// 命令响应类型。 - /// 要发送的命令。 - /// 命令响应。 - /// 该测试桩不支持此成员。 - public TResponse SendCommand(GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:异步发送 CQRS 查询并返回结果。 - /// - /// 查询结果类型。 - /// 要发送的查询。 - /// 取消令牌。 - /// 查询结果任务。 - /// 该测试桩不支持此成员。 - public ValueTask SendQueryAsync( - GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query, - CancellationToken cancellationToken = default) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:同步发送 CQRS 查询并返回结果。 - /// - /// 查询结果类型。 - /// 要发送的查询。 - /// 查询结果。 - /// 该测试桩不支持此成员。 - public TResponse SendQuery(GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:异步发布 CQRS 通知。 - /// - /// 通知类型。 - /// 要发布的通知。 - /// 取消令牌。 - /// 通知发布任务。 - /// 该测试桩不支持此成员。 - public ValueTask PublishAsync(TNotification notification, - CancellationToken cancellationToken = default) where TNotification : INotification - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:创建 CQRS 流式请求响应序列。 - /// - /// 流式响应元素类型。 - /// 流式请求。 - /// 取消令牌。 - /// 异步响应流。 - /// 该测试桩不支持此成员。 - public IAsyncEnumerable CreateStream( - IStreamRequest request, - CancellationToken cancellationToken = default) - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:异步发送无返回值 CQRS 命令。 - /// - /// 命令类型。 - /// 要发送的命令。 - /// 取消令牌。 - /// 命令发送任务。 - /// 该测试桩不支持此成员。 - public ValueTask SendAsync(TCommand command, CancellationToken cancellationToken = default) - where TCommand : IRequest - { - throw new NotSupportedException(); - } - - /// - /// 测试桩:异步发送带返回值的 CQRS 请求。 - /// - /// 响应类型。 - /// 要发送的请求。 - /// 取消令牌。 - /// 请求响应任务。 - /// 该测试桩不支持此成员。 - public ValueTask SendAsync(IRequest command, - CancellationToken cancellationToken = default) - { - throw new NotSupportedException(); - } - - /// - /// 发送命令 - /// - /// 命令对象 - public void SendCommand(ICommand command) - { - } - - /// - /// 发送带返回值的命令 - /// - /// 返回值类型 - /// 命令对象 - /// 命令执行结果 - public TResult SendCommand(ICommand command) - { - return default!; - } - - public Task SendCommandAsync(IAsyncCommand command) - { - return Task.CompletedTask; - } - - public Task SendCommandAsync(IAsyncCommand command) - { - return (Task)Task.CompletedTask; - } - - /// - /// 发送查询请求 - /// - /// 查询结果类型 - /// 查询对象 - /// 查询结果 - public TResult SendQuery(IQuery query) - { - return default!; - } - - /// - /// 异步发送查询请求 - /// - /// 查询结果类型 - /// 异步查询对象 - /// 查询结果 - public Task SendQueryAsync(IAsyncQuery query) - { - return (Task)Task.CompletedTask; - } - - /// - /// 获取环境对象 - /// - /// 环境对象 - public IEnvironment GetEnvironment() - { - return Environment; - } -} diff --git a/GFramework.Core.Tests/Architectures/TestArchitecture.cs b/GFramework.Core.Tests/Architectures/TestArchitecture.cs new file mode 100644 index 00000000..686f44ad --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestArchitecture.cs @@ -0,0 +1,16 @@ +using GFramework.Core.Architectures; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 提供给 的最小架构测试桩。 +/// +public class TestArchitecture : Architecture +{ + /// + /// 保持空初始化流程,便于测试仅验证 的上下文绑定行为。 + /// + protected override void OnInitialize() + { + } +} diff --git a/GFramework.Core.Tests/Architectures/TestArchitectureContext.cs b/GFramework.Core.Tests/Architectures/TestArchitectureContext.cs new file mode 100644 index 00000000..cda590fd --- /dev/null +++ b/GFramework.Core.Tests/Architectures/TestArchitectureContext.cs @@ -0,0 +1,427 @@ +using GFramework.Core.Abstractions.Architectures; +using GFramework.Core.Abstractions.Command; +using GFramework.Core.Abstractions.Environment; +using GFramework.Core.Abstractions.Events; +using GFramework.Core.Abstractions.Ioc; +using GFramework.Core.Abstractions.Model; +using GFramework.Core.Abstractions.Query; +using GFramework.Core.Abstractions.Systems; +using GFramework.Core.Abstractions.Utility; +using GFramework.Core.Command; +using GFramework.Core.Environment; +using GFramework.Core.Events; +using GFramework.Core.Ioc; +using GFramework.Core.Query; +using GFramework.Cqrs.Abstractions.Cqrs; +using ICommand = GFramework.Core.Abstractions.Command.ICommand; + +namespace GFramework.Core.Tests.Architectures; + +/// +/// 为 提供最小可用的架构上下文测试桩。 +/// +/// +/// 该类型只实现当前测试切片会触达的基础行为,其余 CQRS 入口显式抛出 , +/// 避免测试误把未覆盖能力当成可用实现。 +/// +public class TestArchitectureContext : IArchitectureContext +{ + private readonly MicrosoftDiContainer _container = new(); + + /// + /// 获取用于解析测试服务的依赖注入容器。 + /// + public IIocContainer Container => _container; + + /// + /// 获取测试事件总线实例。 + /// + public IEventBus EventBus => new EventBus(); + + /// + /// 获取测试命令执行器实例。 + /// + public ICommandExecutor CommandExecutor => new CommandExecutor(); + + /// + /// 获取测试查询执行器实例。 + /// + public IQueryExecutor QueryExecutor => new QueryExecutor(); + + /// + /// 获取默认测试环境对象。 + /// + public IEnvironment Environment => new DefaultEnvironment(); + + /// + /// 获取指定类型的服务实例。 + /// + /// 服务类型。 + /// 已注册的服务实例。 + /// 未注册服务时抛出。 + public TService GetService() where TService : class + { + return _container.GetRequired(); + } + + /// + /// 获取指定类型的所有服务实例。 + /// + /// 服务类型。 + /// 服务实例列表。 + public IReadOnlyList GetServices() where TService : class + { + return _container.GetAll(); + } + + /// + /// 获取指定类型的模型实例。 + /// + /// 模型类型。 + /// 已注册的模型实例。 + /// 未注册模型时抛出。 + public TModel GetModel() where TModel : class, IModel + { + return _container.GetRequired(); + } + + /// + /// 获取指定类型的所有模型实例。 + /// + /// 模型类型。 + /// 模型实例列表。 + public IReadOnlyList GetModels() where TModel : class, IModel + { + return _container.GetAll(); + } + + /// + /// 获取指定类型的系统实例。 + /// + /// 系统类型。 + /// 已注册的系统实例。 + /// 未注册系统时抛出。 + public TSystem GetSystem() where TSystem : class, ISystem + { + return _container.GetRequired(); + } + + /// + /// 获取指定类型的所有系统实例。 + /// + /// 系统类型。 + /// 系统实例列表。 + public IReadOnlyList GetSystems() where TSystem : class, ISystem + { + return _container.GetAll(); + } + + /// + /// 获取指定类型的工具实例。 + /// + /// 工具类型。 + /// 已注册的工具实例。 + /// 未注册工具时抛出。 + public virtual TUtility GetUtility() where TUtility : class, IUtility + { + return _container.GetRequired(); + } + + /// + /// 获取指定类型的所有工具实例。 + /// + /// 工具类型。 + /// 工具实例列表。 + public IReadOnlyList GetUtilities() where TUtility : class, IUtility + { + return _container.GetAll(); + } + + /// + /// 获取指定类型的所有服务实例,并按优先级排序。 + /// + /// 服务类型。 + /// 按优先级排序后的服务实例列表。 + public IReadOnlyList GetServicesByPriority() where TService : class + { + return _container.GetAllByPriority(); + } + + /// + /// 获取指定类型的所有系统实例,并按优先级排序。 + /// + /// 系统类型。 + /// 按优先级排序后的系统实例列表。 + public IReadOnlyList GetSystemsByPriority() where TSystem : class, ISystem + { + return _container.GetAllByPriority(); + } + + /// + /// 获取指定类型的所有模型实例,并按优先级排序。 + /// + /// 模型类型。 + /// 按优先级排序后的模型实例列表。 + public IReadOnlyList GetModelsByPriority() where TModel : class, IModel + { + return _container.GetAllByPriority(); + } + + /// + /// 获取指定类型的所有工具实例,并按优先级排序。 + /// + /// 工具类型。 + /// 按优先级排序后的工具实例列表。 + public IReadOnlyList GetUtilitiesByPriority() where TUtility : class, IUtility + { + return _container.GetAllByPriority(); + } + + /// + /// 发送无参数事件。 + /// + /// 事件类型。 + public void SendEvent() where TEvent : new() + { + } + + /// + /// 发送带参数事件。 + /// + /// 事件类型。 + /// 事件实例。 + public void SendEvent(TEvent e) where TEvent : class + { + } + + /// + /// 注册事件处理器。 + /// + /// 事件类型。 + /// 事件处理委托。 + /// 用于测试的空注销句柄。 + public IUnRegister RegisterEvent(Action handler) + { + return new DefaultUnRegister(() => { }); + } + + /// + /// 取消注册事件处理器。 + /// + /// 事件类型。 + /// 事件处理委托。 + public void UnRegisterEvent(Action onEvent) + { + } + + /// + /// 测试桩:异步发送统一 CQRS 请求。 + /// + /// 响应类型。 + /// 要发送的请求。 + /// 取消令牌。 + /// 请求响应任务。 + /// 该测试桩不支持此成员。 + public ValueTask SendRequestAsync( + IRequest request, + CancellationToken cancellationToken = default) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:同步发送统一 CQRS 请求。 + /// + /// 响应类型。 + /// 要发送的请求。 + /// 请求响应。 + /// 该测试桩不支持此成员。 + public TResponse SendRequest(IRequest request) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:异步发送 CQRS 命令并返回响应。 + /// + /// 命令响应类型。 + /// 要发送的命令。 + /// 取消令牌。 + /// 命令响应任务。 + /// 该测试桩不支持此成员。 + public ValueTask SendCommandAsync( + GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command, + CancellationToken cancellationToken = default) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:同步发送 CQRS 命令并返回响应。 + /// + /// 命令响应类型。 + /// 要发送的命令。 + /// 命令响应。 + /// 该测试桩不支持此成员。 + public TResponse SendCommand(GFramework.Cqrs.Abstractions.Cqrs.Command.ICommand command) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:异步发送 CQRS 查询并返回结果。 + /// + /// 查询结果类型。 + /// 要发送的查询。 + /// 取消令牌。 + /// 查询结果任务。 + /// 该测试桩不支持此成员。 + public ValueTask SendQueryAsync( + GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query, + CancellationToken cancellationToken = default) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:同步发送 CQRS 查询并返回结果。 + /// + /// 查询结果类型。 + /// 要发送的查询。 + /// 查询结果。 + /// 该测试桩不支持此成员。 + public TResponse SendQuery(GFramework.Cqrs.Abstractions.Cqrs.Query.IQuery query) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:异步发布 CQRS 通知。 + /// + /// 通知类型。 + /// 要发布的通知。 + /// 取消令牌。 + /// 通知发布任务。 + /// 该测试桩不支持此成员。 + public ValueTask PublishAsync( + TNotification notification, + CancellationToken cancellationToken = default) where TNotification : INotification + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:创建 CQRS 流式请求响应序列。 + /// + /// 流式响应元素类型。 + /// 流式请求。 + /// 取消令牌。 + /// 异步响应流。 + /// 该测试桩不支持此成员。 + public IAsyncEnumerable CreateStream( + IStreamRequest request, + CancellationToken cancellationToken = default) + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:异步发送无返回值 CQRS 命令。 + /// + /// 命令类型。 + /// 要发送的命令。 + /// 取消令牌。 + /// 命令发送任务。 + /// 该测试桩不支持此成员。 + public ValueTask SendAsync(TCommand command, CancellationToken cancellationToken = default) + where TCommand : IRequest + { + throw new NotSupportedException(); + } + + /// + /// 测试桩:异步发送带返回值的 CQRS 请求。 + /// + /// 响应类型。 + /// 要发送的请求。 + /// 取消令牌。 + /// 请求响应任务。 + /// 该测试桩不支持此成员。 + public ValueTask SendAsync( + IRequest command, + CancellationToken cancellationToken = default) + { + throw new NotSupportedException(); + } + + /// + /// 发送旧版命令。 + /// + /// 命令对象。 + public void SendCommand(ICommand command) + { + } + + /// + /// 发送旧版带返回值命令。 + /// + /// 返回值类型。 + /// 命令对象。 + /// 测试桩默认返回值。 + public TResult SendCommand(ICommand command) + { + return default!; + } + + /// + /// 异步发送旧版命令。 + /// + /// 命令对象。 + /// 已完成任务。 + public Task SendCommandAsync(IAsyncCommand command) + { + return Task.CompletedTask; + } + + /// + /// 异步发送旧版带返回值命令。 + /// + /// 返回值类型。 + /// 命令对象。 + /// 包含测试桩默认返回值的任务。 + public Task SendCommandAsync(IAsyncCommand command) + { + return Task.FromResult(default(TResult)!); + } + + /// + /// 发送旧版查询请求。 + /// + /// 查询结果类型。 + /// 查询对象。 + /// 测试桩默认返回值。 + public TResult SendQuery(IQuery query) + { + return default!; + } + + /// + /// 异步发送旧版查询请求。 + /// + /// 查询结果类型。 + /// 异步查询对象。 + /// 包含测试桩默认返回值的任务。 + public Task SendQueryAsync(IAsyncQuery query) + { + return Task.FromResult(default(TResult)!); + } + + /// + /// 获取当前环境对象。 + /// + /// 默认测试环境对象。 + public IEnvironment GetEnvironment() + { + return Environment; + } +}