Why does enabling JetBrains dotCover cause NUnit test to fail?

418 views Asked by At

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");
                }
            }
        }
    }
}
1

There are 1 answers

1
hwaien On BEST ANSWER

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:

[Test]
public void Test()
{
    using (var signal = new ManualResetEvent(false))
    {
        var win32 = Mock.Create<IWin32Api>();

        Mock.Arrange(() => win32.FreeResource())
            .DoInstead(() => { signal.Set(); });

        new Action(() => { new Disposable(win32); })();

        GC.Collect();

        GC.WaitForPendingFinalizers();

        if (!signal.WaitOne(TimeSpan.FromMinutes(1)))
        {
            Assert.Fail("IWin32Api.FreeResource never called");
        }
    }
}

The test fails if I replace new Action(() => { new Disposable(win32); })(); with new Disposable(win32); or with var d = new Disposable(win32); d = null;