GeWuYou fb14d7122c docs(style): 更新文档中的命名空间导入格式
- 将所有小写的命名空间导入更正为首字母大写格式
- 统一 GFramework 框架的命名空间引用规范
- 修复 core、ecs、godot 等模块的命名空间导入错误
- 标准化文档示例代码中的 using 语句格式
- 确保所有文档中的命名空间引用保持一致性
- 更新 global using 语句以匹配正确的命名空间格式
2026-03-10 07:18:49 +08:00

85 lines
2.3 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.Events;
using NUnit.Framework;
namespace GFramework.Core.Tests.Events;
/// <summary>
/// EventBus测试类用于验证事件总线的各种功能
/// </summary>
[TestFixture]
public class EventBusTests
{
/// <summary>
/// 测试设置方法在每个测试方法执行前初始化EventBus实例
/// </summary>
[SetUp]
public void SetUp()
{
_eventBus = new EventBus();
}
private EventBus _eventBus = null!;
/// <summary>
/// 测试注册事件处理器的功能
/// 验证注册的处理器能够在发送对应事件时被正确调用
/// </summary>
[Test]
public void Register_Should_Add_Handler()
{
var called = false;
_eventBus.Register<EventBusTestsEvent>(@event => { called = true; });
_eventBus.Send<EventBusTestsEvent>();
Assert.That(called, Is.True);
}
/// <summary>
/// 测试注销事件处理器的功能
/// 验证已注册的处理器在注销后不会再被调用
/// </summary>
[Test]
public void UnRegister_Should_Remove_Handler()
{
var count = 0;
Action<EventBusTestsEvent> handler = @event => { count++; };
_eventBus.Register(handler);
_eventBus.Send<EventBusTestsEvent>();
// 验证处理器被调用一次
Assert.That(count, Is.EqualTo(1));
_eventBus.UnRegister(handler);
_eventBus.Send<EventBusTestsEvent>();
// 验证处理器在注销后不再被调用
Assert.That(count, Is.EqualTo(1));
}
/// <summary>
/// 测试发送事件时调用所有处理器的功能
/// 验证同一事件类型的多个处理器都能被正确调用
/// </summary>
[Test]
public void SendEvent_Should_Invoke_All_Handlers()
{
var count1 = 0;
var count2 = 0;
_eventBus.Register<EventBusTestsEvent>(@event => { count1++; });
_eventBus.Register<EventBusTestsEvent>(@event => { count2++; });
_eventBus.Send<EventBusTestsEvent>();
// 验证所有处理器都被调用一次
Assert.That(count1, Is.EqualTo(1));
Assert.That(count2, Is.EqualTo(1));
}
}
/// <summary>
/// EventBus测试专用事件类
/// </summary>
public class EventBusTestsEvent
{
}