我目前正在审查一个使用 MSTest 并实现 IDisposable 的测试类。测试本身正在测试自定义客户端并具有
的实例MockHttpMessageHandler by RichardSzalay.MockHttp
它实现了 IDisposable 接口(interface)。
以下代码已添加到类的底部,并在每次测试后调用。我想确认什么调用了测试类中声明的 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();
}
}
关于c# - c# MSTest 单元测试中什么调用 Dispose() 方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54182479/