using GFramework.Core.Abstractions.CQRS.Command;
using GFramework.Core.Command;
using NUnit.Framework;
namespace GFramework.Core.Tests.Command;
///
/// CommandBus类的单元测试
/// 测试内容包括:
/// - Send方法执行命令
/// - Send方法处理null命令
/// - Send方法(带返回值)返回值
/// - Send方法(带返回值)处理null命令
/// - SendAsync方法执行异步命令
/// - SendAsync方法处理null异步命令
/// - SendAsync方法(带返回值)返回值
/// - SendAsync方法(带返回值)处理null异步命令
///
[TestFixture]
public class CommandExecutorTests
{
[SetUp]
public void SetUp()
{
_commandExecutor = new CommandExecutor();
}
private CommandExecutor _commandExecutor = null!;
///
/// 测试Send方法执行命令
///
[Test]
public void Send_Should_Execute_Command()
{
var input = new TestCommandInput { Value = 42 };
var command = new TestCommand(input);
Assert.DoesNotThrow(() => _commandExecutor.Send(command));
Assert.That(command.Executed, Is.True);
Assert.That(command.ExecutedValue, Is.EqualTo(42));
}
///
/// 测试Send方法处理null命令时抛出ArgumentNullException异常
///
[Test]
public void Send_WithNullCommand_Should_ThrowArgumentNullException()
{
Assert.Throws(() => _commandExecutor.Send(null!));
}
///
/// 测试Send方法(带返回值)正确返回值
///
[Test]
public void Send_WithResult_Should_Return_Value()
{
var input = new TestCommandInput { Value = 100 };
var command = new TestCommandWithResult(input);
var result = _commandExecutor.Send(command);
Assert.That(command.Executed, Is.True);
Assert.That(result, Is.EqualTo(200));
}
///
/// 测试Send方法(带返回值)处理null命令时抛出ArgumentNullException异常
///
[Test]
public void Send_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
{
Assert.Throws(() => _commandExecutor.Send(null!));
}
///
/// 测试SendAsync方法执行异步命令
///
[Test]
public async Task SendAsync_Should_Execute_AsyncCommand()
{
var input = new TestCommandInput { Value = 42 };
var command = new TestAsyncCommand(input);
await _commandExecutor.SendAsync(command);
Assert.That(command.Executed, Is.True);
Assert.That(command.ExecutedValue, Is.EqualTo(42));
}
///
/// 测试SendAsync方法处理null异步命令时抛出ArgumentNullException异常
///
[Test]
public void SendAsync_WithNullCommand_Should_ThrowArgumentNullException()
{
Assert.ThrowsAsync(async () => await _commandExecutor.SendAsync(null!));
}
///
/// 测试SendAsync方法(带返回值)正确返回值
///
[Test]
public async Task SendAsync_WithResult_Should_Return_Value()
{
var input = new TestCommandInput { Value = 100 };
var command = new TestAsyncCommandWithResult(input);
var result = await _commandExecutor.SendAsync(command);
Assert.That(command.Executed, Is.True);
Assert.That(result, Is.EqualTo(200));
}
///
/// 测试SendAsync方法(带返回值)处理null异步命令时抛出ArgumentNullException异常
///
[Test]
public void SendAsync_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
{
Assert.ThrowsAsync(async () => await _commandExecutor.SendAsync(null!));
}
}
///
/// 测试用命令输入类,实现ICommandInput接口
///
public sealed class TestCommandInput : ICommandInput
{
///
/// 获取或设置值
///
public int Value { get; init; }
}
///
/// 测试用命令类,继承AbstractCommand
///
public sealed class TestCommand : AbstractCommand
{
///
/// 构造函数
///
/// 命令输入
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;
}
}
///
/// 测试用带返回值的命令类,继承AbstractCommand
///
public sealed class TestCommandWithResult : AbstractCommand
{
///
/// 构造函数
///
/// 命令输入
public TestCommandWithResult(TestCommandInput input) : base(input)
{
}
///
/// 获取命令是否已执行
///
public bool Executed { get; private set; }
///
/// 执行命令并返回结果的重写方法
///
/// 命令输入
/// 执行结果
protected override int OnExecute(TestCommandInput input)
{
Executed = true;
return input.Value * 2;
}
}
///
/// 测试用异步命令类,继承AbstractAsyncCommand
///
public sealed class TestAsyncCommand : AbstractAsyncCommand
{
///
/// 构造函数
///
/// 命令输入
public TestAsyncCommand(TestCommandInput input) : base(input)
{
}
///
/// 获取命令是否已执行
///
public bool Executed { get; private set; }
///
/// 获取执行的值
///
public int ExecutedValue { get; private set; }
///
/// 执行异步命令的重写方法
///
/// 命令输入
/// 表示异步操作的任务
protected override Task OnExecuteAsync(TestCommandInput input)
{
Executed = true;
ExecutedValue = 42;
return Task.CompletedTask;
}
}
///
/// 测试用带返回值的异步命令类,继承AbstractAsyncCommand
///
public sealed class TestAsyncCommandWithResult : AbstractAsyncCommand
{
///
/// 构造函数
///
/// 命令输入
public TestAsyncCommandWithResult(TestCommandInput input) : base(input)
{
}
///
/// 获取命令是否已执行
///
public bool Executed { get; private set; }
///
/// 执行异步命令并返回结果的重写方法
///
/// 命令输入
/// 执行结果的异步任务
protected override Task OnExecuteAsync(TestCommandInput input)
{
Executed = true;
return Task.FromResult(input.Value * 2);
}
}