GeWuYou b49079de3e style(coding-style): 统一代码风格并优化文档格式
- 移除多余using语句和空行,统一代码缩进格式
- 优化注释文档中的缩进和对齐格式
- 简化条件语句和方法实现,提升代码可读性
- 重构协程系统相关类的字段和方法定义格式
- 更新架构服务中容器访问方式的实现
- 调整异步操作类的属性和方法组织结构
- [skip ci]
2026-01-27 20:30:04 +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
{
}