GFramework/GFramework.Core.Tests/rule/ContextAwareTests.cs
GeWuYou 5d11666fd8 test(core): 添加核心组件的单元测试
- 为 ContextAware 功能添加全面的单元测试覆盖
- 增加对枚举扩展生成器的快照测试验证
- 实现环境管理器的完整测试用例集
- 添加事件总线功能的核心测试验证
- 为游戏上下文管理添加架构测试
- 扩展注销列表扩展方法的测试覆盖
- 增加注销机制的全面单元测试验证
- [skip ci]
2026-01-15 14:36:30 +08:00

76 lines
2.2 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 GFramework.Core.Abstractions.architecture;
using GFramework.Core.Abstractions.rule;
using GFramework.Core.rule;
using GFramework.Core.Tests.architecture;
using NUnit.Framework;
namespace GFramework.Core.Tests.rule;
[TestFixture]
public class ContextAwareTests
{
[SetUp]
public void SetUp()
{
_contextAware = new TestContextAware();
_mockContext = new TestArchitectureContext();
}
private TestContextAware _contextAware = null!;
private TestArchitectureContext _mockContext = null!;
[Test]
public void SetContext_Should_Set_Context_Property()
{
IContextAware aware = _contextAware;
aware.SetContext(_mockContext);
Assert.That(_contextAware.PublicContext, Is.SameAs(_mockContext));
}
[Test]
public void SetContext_Should_Call_OnContextReady()
{
IContextAware aware = _contextAware;
aware.SetContext(_mockContext);
Assert.That(_contextAware.OnContextReadyCalled, Is.True);
}
[Test]
public void GetContext_Should_Return_Set_Context()
{
IContextAware aware = _contextAware;
aware.SetContext(_mockContext);
var result = aware.GetContext();
Assert.That(result, Is.SameAs(_mockContext));
}
[Test]
public void GetContext_Should_Return_FirstArchitectureContext_When_Not_Set()
{
// Arrange - 暂时不调用 SetContext让 Context 为 null
IContextAware aware = _contextAware;
// Act - 当 Context 为 null 时,应该返回第一个 Architecture Context
// 由于测试环境中没有实际的 Architecture Context这里只测试调用不会抛出异常
// 在实际使用中,当 Context 为 null 时会调用 GameContext.GetFirstArchitectureContext()
// Assert - 验证在没有设置 Context 时的行为
// 注意:由于测试环境中可能没有 Architecture Context这里我们只测试不抛出异常
Assert.DoesNotThrow(() => aware.GetContext());
}
}
public class TestContextAware : ContextAwareBase
{
public IArchitectureContext? PublicContext => Context;
public bool OnContextReadyCalled { get; private set; }
protected override void OnContextReady()
{
OnContextReadyCalled = true;
}
}