GFramework/GFramework.Core.Tests/tests/ArchitectureInitializationTests.cs
GwWuYou f022f25ae6 refactor(architecture): 优化架构上下文清理机制
- 将测试专用的 ResetForTests 方法重命名为 Clear 并改为公共方法
- 在测试初始化时添加上下文清理确保测试环境纯净
- 在测试清理时使用 try-finally 确保架构正确销毁和上下文清理
- 修复了架构对象可能未正确清理的潜在问题
2025-12-30 19:03:58 +08:00

75 lines
1.9 KiB
C#

using System.Reflection;
using GFramework.Core.Abstractions.enums;
using GFramework.Core.architecture;
using GFramework.Core.Tests.architecture;
using GFramework.Core.Tests.model;
using GFramework.Core.Tests.system;
using NUnit.Framework;
namespace GFramework.Core.Tests.tests;
[TestFixture]
[NonParallelizable]
public class ArchitectureInitializationTests
{
[SetUp]
public void SetUp()
{
GameContext.Clear();
_architecture = new TestArchitecture();
}
[TearDown]
public void TearDown()
{
try
{
_architecture?.Destroy();
}
finally
{
GameContext.Clear();
_architecture = null;
}
}
private TestArchitecture? _architecture;
[Test]
public void Architecture_Should_Initialize_All_Components_Correctly()
{
// Act
_architecture!.Initialize();
// Assert
Assert.That(_architecture.InitCalled, Is.True);
Assert.That(_architecture.Runtime, Is.Not.Null);
var phaseProperty = typeof(Architecture)
.GetProperty("CurrentPhase", BindingFlags.Instance | BindingFlags.NonPublic);
var phase = (ArchitecturePhase)phaseProperty!.GetValue(_architecture)!;
Assert.That(phase, Is.EqualTo(ArchitecturePhase.Ready));
var context = _architecture.Context;
var model = context.GetModel<TestModel>();
Assert.That(model, Is.Not.Null);
Assert.That(model!.Inited, Is.True);
var system = context.GetSystem<TestSystem>();
Assert.That(system, Is.Not.Null);
Assert.That(system!.Inited, Is.True);
}
[Test]
public void Architecture_Should_Register_Context_By_Type()
{
// Act
_architecture!.Initialize();
var ctx = GameContext.GetByType(_architecture!.GetType());
Assert.That(ctx, Is.Not.Null);
}
}