A:common 文件测试

This commit is contained in:
jimmy committed 2025-11-13 18:51:31 +08:00
1 parent 9cfbc3e458
commit fdf9a3c762
7 files changed
+2610

No files matched your search

+204
View File
@@ -0,0 +1,204 @@
using Common.ExpandException;
namespace Common.Test;
/// <summary>
/// ExceptionExpand 自定义异常测试
/// 展示带错误码和建议的异常处理
/// </summary>
[TestClass]
public class ExceptionExpandTests
{
/// <summary>
/// 测试:创建带错误码和建议的异常
/// </summary>
[TestMethod]
public void Constructor_CreatesExceptionWithCodeAndSuggestion()
{
// Arrange & Act
var exception = new ExceptionExpand(1001L, "Process failed", "Retry with admin rights");
// Assert
Assert.AreEqual(1001L, exception.ErrorCode);
Assert.AreEqual("Process failed", exception.Message);
Assert.AreEqual("Retry with admin rights", exception.Suggestion);
}
/// <summary>
/// 测试:创建带内部异常的自定义异常
/// </summary>
[TestMethod]
public void Constructor_WithInnerException_PreservesInnerException()
{
// Arrange
var innerException = new InvalidOperationException("Inner error");
// Act
var exception = new ExceptionExpand(2001L, "Outer error", "Check configuration", innerException);
// Assert
Assert.AreEqual(2001L, exception.ErrorCode);
Assert.AreEqual("Outer error", exception.Message);
Assert.AreEqual("Check configuration", exception.Suggestion);
Assert.AreSame(innerException, exception.InnerException);
}
/// <summary>
/// 测试:ToString 包含所有信息
/// </summary>
[TestMethod]
public void ToString_IncludesAllInformation()
{
// Arrange
var exception = new ExceptionExpand(404L, "Resource not found", "Check the resource ID");
// Act
var message = exception.ToString();
// Assert
Assert.IsTrue(message.Contains("404"));
Assert.IsTrue(message.Contains("Resource not found"));
Assert.IsTrue(message.Contains("Check the resource ID"));
}
/// <summary>
/// 测试:抛出和捕获自定义异常
/// </summary>
[TestMethod]
public void ThrowAndCatch_WorksCorrectly()
{
// Act & Assert
var exception = Assert.ThrowsException<ExceptionExpand>(() =>
{
throw new ExceptionExpand(500L, "Server error", "Contact support");
});
Assert.AreEqual(500L, exception.ErrorCode);
Assert.AreEqual("Server error", exception.Message);
Assert.AreEqual("Contact support", exception.Suggestion);
}
/// <summary>
/// 场景测试:使用预定义的异常枚举
/// 演示如何使用 ExampleExceptionEnum 中的预定义异常
/// </summary>
[TestMethod]
public void Scenario_UsePredefinedExceptions()
{
// Act: 抛出预定义异常
var exception = Assert.ThrowsException<ExceptionExpand>(() =>
{
throw ExampleExceptionEnum.ProcessGetException;
});
// Assert: 验证预定义异常的属性
Assert.AreEqual(100001L, exception.ErrorCode);
Assert.IsTrue(exception.Message.Contains("进程消息获取失败"));
Assert.IsTrue(exception.Suggestion.Contains("管理员权限"));
}
/// <summary>
/// 场景测试:在业务逻辑中使用自定义异常
/// </summary>
[TestMethod]
public void Scenario_UseInBusinessLogic()
{
// Act: 执行可能失败的业务操作
var exception = Assert.ThrowsException<ExceptionExpand>(() =>
{
ProcessBusinessOperation(-1);
});
// Assert
Assert.AreEqual(4001L, exception.ErrorCode);
Assert.AreEqual("Invalid user ID", exception.Message);
Assert.AreEqual("User ID must be positive", exception.Suggestion);
}
/// <summary>
/// 场景测试:异常链
/// 演示如何使用 InnerException 构建异常链
/// </summary>
[TestMethod]
public void Scenario_ExceptionChain()
{
// Arrange: 创建异常链
var databaseException = new InvalidOperationException("Database connection failed");
var serviceException = new ExceptionExpand(
5001L,
"Service unavailable",
"Check database connection",
databaseException
);
// Act: 捕获并检查异常链
var exception = Assert.ThrowsException<ExceptionExpand>(() =>
{
throw serviceException;
});
// Assert: 验证异常链
Assert.AreEqual(5001L, exception.ErrorCode);
Assert.IsNotNull(exception.InnerException);
Assert.IsInstanceOfType(exception.InnerException, typeof(InvalidOperationException));
Assert.AreEqual("Database connection failed", exception.InnerException.Message);
}
/// <summary>
/// 场景测试:根据错误码进行不同处理
/// </summary>
[TestMethod]
public void Scenario_HandleByErrorCode()
{
// Arrange: 模拟不同的错误场景
var errors = new[]
{
new ExceptionExpand(400L, "Bad request", "Check input parameters"),
new ExceptionExpand(404L, "Not found", "Verify resource ID"),
new ExceptionExpand(500L, "Internal error", "Contact support")
};
// Act & Assert: 根据错误码分类处理
foreach (var error in errors)
{
var handled = HandleError(error);
if (error.ErrorCode == 400L)
Assert.AreEqual("Client error", handled);
else if (error.ErrorCode == 404L)
Assert.AreEqual("Client error", handled);
else if (error.ErrorCode == 500L)
Assert.AreEqual("Server error", handled);
}
}
#region 测试辅助方法
/// <summary>
/// 模拟业务操作
/// </summary>
private void ProcessBusinessOperation(int userId)
{
if (userId <= 0)
{
throw new ExceptionExpand(4001L, "Invalid user ID", "User ID must be positive");
}
// 正常处理逻辑...
}
/// <summary>
/// 根据错误码处理错误
/// </summary>
private string HandleError(ExceptionExpand exception)
{
return exception.ErrorCode switch
{
>= 400L and < 500L => "Client error",
>= 500L and < 600L => "Server error",
_ => "Unknown error"
};
}
#endregion
}