Set a breakpoint in the Dispose () method and run these tests using debugging. TestMethod1 does not hit the breakpoint during TestMethod2.
As others have noted, this is related to how GC.Net works. If you are going to implement the IDiisposeable interface, you probably want to put your class in a using statement or call .Dispose () so that you have more predictable application behavior.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace UnitTestProject2
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod1()
{
var disposable = new DisposableObject();
disposable.DoSomething();
}
[TestMethod]
public void TestMethod2()
{
using (var disposable = new DisposableObject())
{
disposable.DoSomething();
}
}
}
public class DisposableObject : IDisposable
{
public void Dispose()
{
}
public void DoSomething()
{
}
}
}
source
share