test(core-tests): 简化异步断言包装

- 简化 Architecture、Command、Query 与 AsyncArchitecture 测试中的机械型 async/await 异步断言包装
- 更新 AsyncKeyLockManagerTests 中的 Task 断言写法以消除低风险 analyzer 噪音
This commit is contained in:
gewuyou 2026-04-25 09:46:44 +08:00
parent 9b20a07c0a
commit 67c9359fd2
7 changed files with 16 additions and 16 deletions

View File

@ -99,10 +99,10 @@ public class ArchitectureLifecycleBehaviorTests
{ {
var architecture = new PhaseTrackingArchitecture(() => throw new InvalidOperationException("boom")); var architecture = new PhaseTrackingArchitecture(() => throw new InvalidOperationException("boom"));
var exception = Assert.ThrowsAsync<InvalidOperationException>(async () => await architecture.InitializeAsync()); var exception = Assert.ThrowsAsync<InvalidOperationException>(() => architecture.InitializeAsync());
Assert.That(exception, Is.Not.Null); Assert.That(exception, Is.Not.Null);
Assert.That(architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.FailedInitialization)); Assert.That(architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.FailedInitialization));
Assert.ThrowsAsync<InvalidOperationException>(async () => await architecture.WaitUntilReadyAsync()); Assert.ThrowsAsync<InvalidOperationException>(() => architecture.WaitUntilReadyAsync());
} }
/// <summary> /// <summary>
@ -139,7 +139,7 @@ public class ArchitectureLifecycleBehaviorTests
var destroyOrder = new List<string>(); var destroyOrder = new List<string>();
var architecture = new FailingInitializationArchitecture(destroyOrder); var architecture = new FailingInitializationArchitecture(destroyOrder);
var exception = Assert.ThrowsAsync<InvalidOperationException>(async () => await architecture.InitializeAsync()); var exception = Assert.ThrowsAsync<InvalidOperationException>(() => architecture.InitializeAsync());
Assert.That(exception, Is.Not.Null); Assert.That(exception, Is.Not.Null);
Assert.That(architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.FailedInitialization)); Assert.That(architecture.CurrentPhase, Is.EqualTo(ArchitecturePhase.FailedInitialization));

View File

@ -110,7 +110,7 @@ public class AbstractAsyncCommandTests
var command = new TestAsyncCommandWithExceptionV3(input); var command = new TestAsyncCommandWithExceptionV3(input);
var asyncCommand = (IAsyncCommand)command; var asyncCommand = (IAsyncCommand)command;
Assert.ThrowsAsync<InvalidOperationException>(async () => await asyncCommand.ExecuteAsync()); Assert.ThrowsAsync<InvalidOperationException>(() => asyncCommand.ExecuteAsync());
} }
/// <summary> /// <summary>

View File

@ -94,7 +94,7 @@ public class CommandExecutorTests
[Test] [Test]
public void SendAsync_WithNullCommand_Should_ThrowArgumentNullException() public void SendAsync_WithNullCommand_Should_ThrowArgumentNullException()
{ {
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandExecutor.SendAsync(null!)); Assert.ThrowsAsync<ArgumentNullException>(() => _commandExecutor.SendAsync(null!));
} }
/// <summary> /// <summary>
@ -118,7 +118,7 @@ public class CommandExecutorTests
[Test] [Test]
public void SendAsync_WithResult_AndNullCommand_Should_ThrowArgumentNullException() public void SendAsync_WithResult_AndNullCommand_Should_ThrowArgumentNullException()
{ {
Assert.ThrowsAsync<ArgumentNullException>(async () => await _commandExecutor.SendAsync<int>(null!)); Assert.ThrowsAsync<ArgumentNullException>(() => _commandExecutor.SendAsync<int>(null!));
} }
} }

View File

@ -123,7 +123,7 @@ public sealed class AsyncKeyLockManagerTests
} }
// Assert // Assert
Assert.DoesNotThrowAsync(async () => await Task.WhenAll(tasks)); Assert.DoesNotThrowAsync(() => Task.WhenAll(tasks));
} }
[Test] [Test]
@ -243,7 +243,7 @@ public sealed class AsyncKeyLockManagerTests
manager.Dispose(); manager.Dispose();
// Act & Assert // Act & Assert
Assert.ThrowsAsync<ObjectDisposedException>(async () => await manager.AcquireLockAsync("test-key")); Assert.ThrowsAsync<ObjectDisposedException>(() => manager.AcquireLockAsync("test-key").AsTask());
} }
[Test] [Test]
@ -302,7 +302,7 @@ public sealed class AsyncKeyLockManagerTests
} }
// Assert // Assert
Assert.DoesNotThrowAsync(async () => await Task.WhenAll(tasks)); Assert.DoesNotThrowAsync(() => Task.WhenAll(tasks));
} }
[Test] [Test]

View File

@ -80,7 +80,7 @@ public class AbstractAsyncQueryTests
var query = new TestAsyncQueryWithExceptionV4(input); var query = new TestAsyncQueryWithExceptionV4(input);
var asyncQuery = (IAsyncQuery<int>)query; var asyncQuery = (IAsyncQuery<int>)query;
Assert.ThrowsAsync<InvalidOperationException>(async () => await asyncQuery.DoAsync()); Assert.ThrowsAsync<InvalidOperationException>(() => asyncQuery.DoAsync());
} }
/// <summary> /// <summary>

View File

@ -44,7 +44,7 @@ public class AsyncQueryExecutorTests
[Test] [Test]
public void SendAsync_WithNullQuery_Should_ThrowArgumentNullException() public void SendAsync_WithNullQuery_Should_ThrowArgumentNullException()
{ {
Assert.ThrowsAsync<ArgumentNullException>(async () => await _asyncQueryExecutor.SendAsync<int>(null!)); Assert.ThrowsAsync<ArgumentNullException>(() => _asyncQueryExecutor.SendAsync<int>(null!));
} }
/// <summary> /// <summary>
@ -100,7 +100,7 @@ public class AsyncQueryExecutorTests
var input = new TestAsyncQueryInput { Value = 0 }; var input = new TestAsyncQueryInput { Value = 0 };
var query = new TestAsyncQueryWithException(input); var query = new TestAsyncQueryWithException(input);
Assert.ThrowsAsync<InvalidOperationException>(async () => await _asyncQueryExecutor.SendAsync(query)); Assert.ThrowsAsync<InvalidOperationException>(() => _asyncQueryExecutor.SendAsync(query));
} }
/// <summary> /// <summary>

View File

@ -93,7 +93,7 @@ public class AsyncArchitectureTests : ArchitectureTestsBase<AsyncTestArchitectur
{ {
Architecture!.AddPostRegistrationHook(a => { a.RegisterModel(new FailingModel()); }); Architecture!.AddPostRegistrationHook(a => { a.RegisterModel(new FailingModel()); });
Assert.ThrowsAsync<InvalidOperationException>(async () => await Architecture.InitializeAsync()); Assert.ThrowsAsync<InvalidOperationException>(() => Architecture.InitializeAsync());
Assert.That( Assert.That(
Architecture.CurrentPhase, Architecture.CurrentPhase,
@ -140,7 +140,7 @@ public class AsyncArchitectureTests : ArchitectureTestsBase<AsyncTestArchitectur
a.RegisterModel(new FailingModel()) a.RegisterModel(new FailingModel())
); );
Assert.ThrowsAsync<InvalidOperationException>(async () => await Architecture.InitializeAsync()); Assert.ThrowsAsync<InvalidOperationException>(() => Architecture.InitializeAsync());
AssertInitializationFailed(); AssertInitializationFailed();
} }