2

I currently reviewing a test class that uses MSTest and implements IDisposable. The test itself is testing a custom client and has an instance of

MockHttpMessageHandler by RichardSzalay.MockHttp

which implements the IDisposable interface.

The following code has been added at the bottom of the class and gets called after each test. I am looking to confirm what calls the Dispose method that is declared in the test class

public void Dispose()
{
    _mockHttpHandler.Dispose();
}
eVolve
  • 1,340
  • 1
  • 11
  • 30
  • 4
    I strongly suspect that will depend on which test framework you're using. Please add that information to the question. Also, have you tried debugging and just putting a breakpoint on the `Dispose` call, then looking at the stack trace? – Jon Skeet Jan 14 '19 at 13:31
  • Updated question to include MSTest as the test framework. The callstack only shows the current breakpoint location which is the Dispose method in the test class. – eVolve Jan 14 '19 at 13:46

1 Answers1

4

MSTest performs a type conversion check using the as operator and then calls the Dispose method in this case:

 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();
    }
  }
eVolve
  • 1,340
  • 1
  • 11
  • 30