GeWuYou dda2d8f864 test(core): 添加核心组件单元测试并优化目标框架配置
- 在AsyncTestModel和TestModel中添加override关键字修复方法重写
- 调整GFramework.Core.Tests和GFramework.SourceGenerators.Tests的目标框架顺序为net10.0;net8.0
- 优化SyncArchitectureTests中的注释格式,统一添加前导空格
- 移除TestQuery相关代码文件
- 新增BindablePropertyTests测试绑定属性功能
- 新增CommandBusTests测试命令总线功能
- 新增EasyEventsTests和EventTests测试事件系统功能
- 新增IocContainerTests测试依赖注入容器功能
- 新增ObjectExtensionsTests测试对象扩展方法功能
- 新增ObjectPoolTests测试对象池功能
- 新增OrEventTests测试或事件功能
- 新增QueryBusTests测试查询总线功能
- [skip ci]
2026-01-15 13:53:08 +08:00

88 lines
2.1 KiB
C#

using GFramework.Core.Abstractions.command;
using GFramework.Core.command;
using NUnit.Framework;
namespace GFramework.Core.Tests.command;
[TestFixture]
public class CommandBusTests
{
[SetUp]
public void SetUp()
{
_commandBus = new CommandBus();
}
private CommandBus _commandBus = null!;
[Test]
public void Send_Should_Execute_Command()
{
var input = new TestCommandInput { Value = 42 };
var command = new TestCommand(input);
Assert.DoesNotThrow(() => _commandBus.Send(command));
Assert.That(command.Executed, Is.True);
Assert.That(command.ExecutedValue, Is.EqualTo(42));
}
[Test]
public void Send_WithNullCommand_Should_ThrowArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _commandBus.Send(null!));
}
[Test]
public void Send_WithResult_Should_Return_Value()
{
var input = new TestCommandInput { Value = 100 };
var command = new TestCommandWithResult(input);
var result = _commandBus.Send(command);
Assert.That(command.Executed, Is.True);
Assert.That(result, Is.EqualTo(200));
}
[Test]
public void Send_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
{
Assert.Throws<ArgumentNullException>(() => _commandBus.Send<int>(null!));
}
}
public sealed class TestCommandInput : ICommandInput
{
public int Value { get; init; }
}
public sealed class TestCommand : AbstractCommand<TestCommandInput>
{
public TestCommand(TestCommandInput input) : base(input)
{
}
public bool Executed { get; private set; }
public int ExecutedValue { get; private set; }
protected override void OnExecute(TestCommandInput input)
{
Executed = true;
ExecutedValue = 42;
}
}
public sealed class TestCommandWithResult : AbstractCommand<TestCommandInput, int>
{
public TestCommandWithResult(TestCommandInput input) : base(input)
{
}
public bool Executed { get; private set; }
protected override int OnExecute(TestCommandInput input)
{
Executed = true;
return input.Value * 2;
}
}