c# - 什么在 c# MSTest 单元测试中调用 Dispose() 方法?
问题描述
我目前正在审查一个使用 MSTest 并实现 IDisposable 的测试类。测试本身正在测试一个自定义客户端,并且有一个实例
RichardSzalay.MockHttp 的 MockHttpMessageHandler
它实现了 IDisposable 接口。
以下代码已添加到类的底部,并在每次测试后被调用。我正在寻找确认什么调用了在测试类中声明的 Dispose 方法
public void Dispose()
{
_mockHttpHandler.Dispose();
}
解决方案
MSTest 使用 as 运算符执行类型转换检查,然后在这种情况下调用 Dispose 方法:
private void RunTestCleanupMethod(object classInstance, TestResult result)
{
MethodInfo methodInfo = this.Parent.TestCleanupMethod;
try
{
try
{
if (methodInfo != null)
methodInfo.InvokeAsSynchronousTask(classInstance, (object[]) null);
Queue<MethodInfo> methodInfoQueue = new Queue<MethodInfo>((IEnumerable<MethodInfo>) this.Parent.BaseTestCleanupMethodsQueue);
while (methodInfoQueue.Count > 0)
{
methodInfo = methodInfoQueue.Dequeue();
if (methodInfo != null)
methodInfo.InvokeAsSynchronousTask(classInstance, (object[]) null);
}
}
finally
{
(classInstance as IDisposable)?.Dispose();
}
}
推荐阅读
- java - 使用 java8 流 api 将一个对象转换为另一个
- node.js - 安装 SASS JSON.parse 时出错 无法解析 package.json 数据
- javascript - Stream.io Chat API - 在 Angular 中使用 Stream Chat 和 Observables / RxJS
- sql - 如何将通配符应用于 SQL 中的列列表
- mongodb - 验证设置时出错:只允许一个位置参数
- postgresql - Postgres - 如果无法转换,则将列转换为类型或 Null
- java - 尽管更改了加密文本,但能够在 Java 中解密加密文本
- node.js - Koa 无法获取 ctx.request-body 内部属性的值
- python-3.x - 为什么当我输入超出范围的新值时它会打印“无”?蟒蛇 3
- grpc - Http/2 Pusher 与 gRPC 性能