【发布时间】:2022-11-04 16:05:56
【问题描述】:
我有自定义异常-->
public CustomException(SerializationInfo info, StreamingContext context)
: base(info, context)
{ }
我正在为它写一个单元测试——>
[TestMethod]
[ExpectedException(typeof(CustomException))]
public void TestCustomException()
{
throw new CustomException(info:SerializationInfo, context:StreamingContext);
}
错误显示为 SerializationInfo 和 StreamingContext 是类型并且对于给定的上下文无效。
您能否让我知道从单元测试中抛出自定义异常的正确方法是什么?
【问题讨论】:
-
不遵循您的语法。
info:SerializationInfo和context:StreamingContext应该代表什么?他们来自哪里? -
我想抛出自定义异常,但不确定什么应该是正确的语法,我只是想尝试提及我在 CustomException(SerializationInfo info, StreamingContext context) 中使用的内容
-
创建一个新的
SerializationInfo和一个新的StreamingContext并将它们用作参数 -
虽然不确定你实际实现了什么,但它当然会抛出它。也许您想测试异常中的数据?
-
这是为了满足该自定义异常方法的代码覆盖率
标签: c# unit-testing exception