c# - c# MSTest 单元测试中什么调用 Dispose() 方法?

标签 c# unit-testing asp.net-core mockhttpmessagehandler

我目前正在审查一个使用 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/

相关文章:

c# - 如何获取操作系统名称

c# - 在点网中不可能适用于 int 和 uint 的通用方法吗?

c# - 使用 C# 在 Unity3D 中获取要摆动的对象

C# 异步异常包装

reactjs - 使用 React 测试库/Jest 模拟 axios 帖子

python - 在不使用模拟的情况下检查函数是否已在 Python unittest 中调用的规范方法是什么?

python - 如何正确地对网络应用程序进行单元测试?

c# - ASP.NET 核心 3.1 : does ConfigureAppConfiguration interacts with launchSettings. json?

.net - Entity Framework Core 连接管理

c# - .Net 核心 API 在 linux 机器上的监控