I wrote some code that looks like the following, to unit-test the implementation of a C# finalizer method.
In TeamCity version 2017.1.3, I use a build step with the NUnit runner type to run this test using NUnit.ConsoleRunner.3.7.0.
The test fails if I enable the JetBrains dotCover .NET Coverage tool.
The test passes if I disable .NET Coverage.
What can dotCover possibly be doing to influence test outcome?
using System;
using System.Threading;
using NUnit.Framework;
using Telerik.JustMock;
namespace MyNamespace
{
public interface IWin32Api
{
void FreeResource();
}
public class Disposable
{
private readonly IWin32Api _win32;
public Disposable(IWin32Api win32)
{
_win32 = win32;
}
~Disposable()
{
_win32.FreeResource();
}
}
[TestFixture]
public class TestFixture
{
[Test]
public void Test()
{
using (var signal = new ManualResetEvent(false))
{
var win32 = Mock.Create<IWin32Api>();
Mock.Arrange(() => win32.FreeResource())
.DoInstead(() => { signal.Set(); });
var subjectUnderTest = new Disposable(win32);
subjectUnderTest = null;
GC.Collect();
if (!signal.WaitOne(TimeSpan.FromMinutes(1)))
{
Assert.Fail("IWin32Api.FreeResource never called");
}
}
}
}
}
I still do not understand what's going on, but I was able to fix the problem and get the test to pass by using an immediately-invoked function expression.
The test method now looks like this:
The test fails if I replace
new Action(() => { new Disposable(win32); })();
withnew Disposable(win32);
or withvar d = new Disposable(win32); d = null;