GFramework/GFramework.Core.Tests/rule/ContextAwareTests.cs
GeWuYou 1783245d22 test(rule): 添加上下文感知测试的绑定和清理逻辑
- 在测试设置中添加 GameContext 绑定
- 添加 TearDown 方法用于解绑上下文
- 确保测试后正确清理上下文状态
2026-01-15 22:00:05 +08:00

84 lines
2.5 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.architecture;
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();
GameContext.Bind(typeof(TestArchitectureContext), _mockContext);
}
[TearDown]
public void TearDown()
{
GameContext.Unbind(typeof(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;
}
}