From ed187473ddf97a24f764708066deccc13c520188 Mon Sep 17 00:00:00 2001 From: GeWuYou <95328647+GeWuYou@users.noreply.github.com> Date: Mon, 23 Feb 2026 13:32:59 +0800 Subject: [PATCH] =?UTF-8?q?test(architecture):=20=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=9E=B6=E6=9E=84=E6=9C=8D=E5=8A=A1=E6=B5=8B=E8=AF=95=E4=BB=A5?= =?UTF-8?q?=E9=AA=8C=E8=AF=81=E5=86=85=E7=BD=AE=E6=A8=A1=E5=9D=97=E6=B3=A8?= =?UTF-8?q?=E5=86=8C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 添加了对ArchitectureProperties和GFramework.Core.services的引用 - 实现了RegisterBuiltInServices方法用于注册内置服务 - 修改了构造函数测试以验证容器初始化而非所有服务 - 更新了EventBus、CommandExecutor和QueryExecutor的测试以验证注册后可用性 - 添加了AsyncQueryExecutor可用性测试 - 添加了未注册服务时EventBus为null的测试 - 在多个实例测试中添加了模块注册以确保独立性 - 添加了ModuleManager属性非空测试 - 实现了ECS配置开关控制模块注册的测试 - 移除了TestArchitectureContextV3中的硬编码服务实现 - 更新了ECS相关测试以直接注册EcsWorld到容器中 - 改进了ECS世界获取失败时的错误消息 --- .../architecture/ArchitectureServicesTests.cs | 103 +++++++++++++++--- GFramework.Core.Tests/ecs/EcsAdvancedTests.cs | 16 ++- GFramework.Core.Tests/ecs/EcsBasicTests.cs | 9 +- .../ecs/EcsIntegrationTests.cs | 10 +- 4 files changed, 110 insertions(+), 28 deletions(-) diff --git a/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs b/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs index fcce9a4..1baf59f 100644 --- a/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs +++ b/GFramework.Core.Tests/architecture/ArchitectureServicesTests.cs @@ -5,6 +5,7 @@ using GFramework.Core.Abstractions.environment; using GFramework.Core.Abstractions.events; using GFramework.Core.Abstractions.ioc; using GFramework.Core.Abstractions.model; +using GFramework.Core.Abstractions.properties; using GFramework.Core.Abstractions.query; using GFramework.Core.Abstractions.system; using GFramework.Core.Abstractions.utility; @@ -46,16 +47,19 @@ public class ArchitectureServicesTests private ArchitectureServices? _services; private TestArchitectureContextV3? _context; + private void RegisterBuiltInServices() + { + var properties = new ArchitectureProperties(); + _services!.ModuleManager.RegisterBuiltInModules(_services.Container, properties); + } + /// - /// 测试构造函数初始化所有服务 + /// 测试构造函数初始化容器 /// [Test] - public void Constructor_Should_Initialize_AllServices() + public void Constructor_Should_Initialize_Container() { Assert.That(_services!.Container, Is.Not.Null); - Assert.That(_services.EventBus, Is.Not.Null); - Assert.That(_services.CommandExecutor, Is.Not.Null); - Assert.That(_services.QueryExecutor, Is.Not.Null); } [Test] @@ -66,35 +70,62 @@ public class ArchitectureServicesTests } /// - /// 测试EventBus是EventBus的实例 + /// 测试注册内置服务后EventBus可用 /// [Test] - public void EventBus_Should_Be_Instance_Of_EventBus() + public void After_RegisterBuiltInModules_EventBus_Should_Be_Available() { + RegisterBuiltInServices(); + Assert.That(_services!.EventBus, Is.InstanceOf()); Assert.That(_services.EventBus, Is.InstanceOf()); } /// - /// 测试CommandBus是CommandBus的实例 + /// 测试注册内置服务后CommandExecutor可用 /// [Test] - public void CommandBus_Should_Be_Instance_Of_CommandBus() + public void After_RegisterBuiltInModules_CommandExecutor_Should_Be_Available() { + RegisterBuiltInServices(); + Assert.That(_services!.CommandExecutor, Is.InstanceOf()); Assert.That(_services.CommandExecutor, Is.InstanceOf()); } /// - /// 测试QueryBus是QueryBus的实例 + /// 测试注册内置服务后QueryExecutor可用 /// [Test] - public void QueryBus_Should_Be_Instance_Of_QueryBus() + public void After_RegisterBuiltInModules_QueryExecutor_Should_Be_Available() { + RegisterBuiltInServices(); + Assert.That(_services!.QueryExecutor, Is.InstanceOf()); Assert.That(_services.QueryExecutor, Is.InstanceOf()); } + /// + /// 测试注册内置服务后AsyncQueryExecutor可用 + /// + [Test] + public void After_RegisterBuiltInModules_AsyncQueryExecutor_Should_Be_Available() + { + RegisterBuiltInServices(); + + Assert.That(_services!.AsyncQueryExecutor, Is.InstanceOf()); + Assert.That(_services.AsyncQueryExecutor, Is.InstanceOf()); + } + + /// + /// 测试未注册服务时EventBus为null + /// + [Test] + public void Without_RegisterBuiltInModules_EventBus_Should_Be_Null() + { + Assert.That(_services!.EventBus, Is.Null); + } + /// /// 测试SetContext设置内部Context字段 /// @@ -186,8 +217,13 @@ public class ArchitectureServicesTests [Test] public void Multiple_Instances_Should_Have_Independent_EventBus() { + var properties = new ArchitectureProperties(); + var services1 = new ArchitectureServices(); + services1.ModuleManager.RegisterBuiltInModules(services1.Container, properties); + var services2 = new ArchitectureServices(); + services2.ModuleManager.RegisterBuiltInModules(services2.Container, properties); Assert.That(services1.EventBus, Is.Not.SameAs(services2.EventBus)); } @@ -198,8 +234,13 @@ public class ArchitectureServicesTests [Test] public void Multiple_Instances_Should_Have_Independent_CommandBus() { + var properties = new ArchitectureProperties(); + var services1 = new ArchitectureServices(); + services1.ModuleManager.RegisterBuiltInModules(services1.Container, properties); + var services2 = new ArchitectureServices(); + services2.ModuleManager.RegisterBuiltInModules(services2.Container, properties); Assert.That(services1.CommandExecutor, Is.Not.SameAs(services2.CommandExecutor)); } @@ -210,11 +251,46 @@ public class ArchitectureServicesTests [Test] public void Multiple_Instances_Should_Have_Independent_QueryBus() { + var properties = new ArchitectureProperties(); + var services1 = new ArchitectureServices(); + services1.ModuleManager.RegisterBuiltInModules(services1.Container, properties); + var services2 = new ArchitectureServices(); + services2.ModuleManager.RegisterBuiltInModules(services2.Container, properties); Assert.That(services1.QueryExecutor, Is.Not.SameAs(services2.QueryExecutor)); } + + /// + /// 测试ModuleManager属性不为空 + /// + [Test] + public void ModuleManager_Should_Not_Be_Null() + { + Assert.That(_services!.ModuleManager, Is.Not.Null); + } + + /// + /// 测试EnableEcs配置开关 + /// + [Test] + public void EnableEcs_Should_Control_Ecs_Module_Registration() + { + var propertiesWithEcs = new ArchitectureProperties { EnableEcs = true }; + var propertiesWithoutEcs = new ArchitectureProperties { EnableEcs = false }; + + var servicesWithEcs = new ArchitectureServices(); + servicesWithEcs.ModuleManager.RegisterBuiltInModules(servicesWithEcs.Container, propertiesWithEcs); + + var servicesWithoutEcs = new ArchitectureServices(); + servicesWithoutEcs.ModuleManager.RegisterBuiltInModules(servicesWithoutEcs.Container, propertiesWithoutEcs); + + var modulesWithEcs = servicesWithEcs.ModuleManager.GetModules(); + var modulesWithoutEcs = servicesWithoutEcs.ModuleManager.GetModules(); + + Assert.That(modulesWithEcs.Count, Is.GreaterThan(modulesWithoutEcs.Count)); + } } #region Test Classes @@ -225,11 +301,6 @@ public class TestArchitectureContextV3 : IArchitectureContext private readonly DefaultEnvironment _environment = new(); public int Id { get; init; } - public IIocContainer Container => _container; - public IEventBus EventBus => new EventBus(); - public ICommandExecutor CommandExecutor => new CommandExecutor(); - public IQueryExecutor QueryExecutor => new QueryExecutor(); - public TService? GetService() where TService : class { return _container.Get(); diff --git a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs index 972df44..577f7e1 100644 --- a/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs +++ b/GFramework.Core.Tests/ecs/EcsAdvancedTests.cs @@ -266,20 +266,24 @@ public class EcsAdvancedTests [Test] public void InitializeEcs_CalledTwice_Should_BeIdempotent() { - _context!.InitializeEcs(); - var ecsWorld1 = _context.GetEcsWorld(); - - Assert.DoesNotThrow(() => _context.InitializeEcs()); + _ecsWorld = new EcsWorld(); + _container!.Register(_ecsWorld); + _container.Register(_ecsWorld); + var ecsWorld1 = _context!.GetEcsWorld(); var ecsWorld2 = _context.GetEcsWorld(); + Assert.That(ecsWorld2, Is.SameAs(ecsWorld1), "Should return same world instance"); } [Test] public void GetEcsWorld_Should_ReturnIEcsWorld() { - _context!.InitializeEcs(); - var ecsWorld = _context.GetEcsWorld(); + _ecsWorld = new EcsWorld(); + _container!.Register(_ecsWorld); + _container.Register(_ecsWorld); + + var ecsWorld = _context!.GetEcsWorld(); Assert.That(ecsWorld, Is.InstanceOf()); Assert.That(ecsWorld, Is.InstanceOf()); diff --git a/GFramework.Core.Tests/ecs/EcsBasicTests.cs b/GFramework.Core.Tests/ecs/EcsBasicTests.cs index 6fd1dce..036345f 100644 --- a/GFramework.Core.Tests/ecs/EcsBasicTests.cs +++ b/GFramework.Core.Tests/ecs/EcsBasicTests.cs @@ -85,8 +85,11 @@ public class EcsBasicTests [Experimental("GFrameworkECS")] public void Test_01_InitializeEcs_Should_Create_EcsWorld() { - _context!.InitializeEcs(); - var ecsWorld = _context.GetEcsWorld(); + _ecsWorld = new EcsWorld(); + _container!.Register(_ecsWorld); + _container.Register(_ecsWorld); + + var ecsWorld = _context!.GetEcsWorld(); Assert.That(ecsWorld, Is.Not.Null, "EcsWorld should be created"); Assert.That(ecsWorld.EntityCount, Is.EqualTo(0), "Initial entity count should be 0"); @@ -221,6 +224,6 @@ public class EcsBasicTests public void Test_08_GetEcsWorld_Without_Initialize_Should_Throw() { Assert.Throws(() => { _context!.GetEcsWorld(); }, - "Getting EcsWorld without initialization should throw"); + "ECS World not initialized. Enable ECS in configuration."); } } \ No newline at end of file diff --git a/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs b/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs index 6a70b37..e670393 100644 --- a/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs +++ b/GFramework.Core.Tests/ecs/EcsIntegrationTests.cs @@ -84,8 +84,11 @@ public class EcsIntegrationTests [Test] public void InitializeEcs_Should_Create_EcsWorld() { - _context!.InitializeEcs(); - var ecsWorld = _context.GetEcsWorld(); + _ecsWorld = new EcsWorld(); + _container!.Register(_ecsWorld); + _container.Register(_ecsWorld); + + var ecsWorld = _context!.GetEcsWorld(); Assert.That(ecsWorld, Is.Not.Null); Assert.That(ecsWorld.EntityCount, Is.EqualTo(0)); @@ -246,7 +249,8 @@ public class EcsIntegrationTests [Test] public void GetEcsWorld_Without_Initialize_Should_Throw() { - Assert.Throws(() => { _context!.GetEcsWorld(); }); + Assert.Throws(() => { _context!.GetEcsWorld(); }, + "ECS World not initialized. Enable ECS in configuration."); } ///