Using declaration causes InvalidProgramException with JustMock

1 Answer 186 Views
General Discussions
Anthony
Top achievements
Rank 1
Anthony asked on 11 Oct 2022, 08:25 PM | edited on 11 Oct 2022, 08:43 PM

I've run into an interesting problem with JustMock. In a nutshell, if you use the C# using declaration (as opposed to a using block), and have JustMock mocking anything in the same assembly, the runtime will throw an InvalidProgramException.

I was able to isolate it down to the narrowest possible case as a proof of point. Using NUnit as the testing framework (I haven't tried with others), and a reference to JustMock, the following code will compile successfully, but at runtime will throw an InvalidOperationException when trying to run the unit test.

[TestFixture]
public class Fixture
{
    public interface ITest { }

    public class TestClass : IDisposable
    {
        public void Dispose()
        {
        }
    }

    [Test]
    public async Task Test()
    {
        ITest mock = Mock.Create<ITest>();
        using TestClass test = new();
    }
}

I've played with various permutations of the problem, and it happen on .NET 4.8 (which we are using) as well as .NET 6.0, and with the version of JustMock we are using (2020.2.616.1) as well as the latest commercial release (2022.3.912.3).

For the problem to occur, the using declaration and the mock creation need not be in the same test, or even in the same fixture, but the test with the using declaration will always fail with the exception. If the async modifier is removed from the unit test, the problem goes away.

Turning off the profiler makes the problem go away, as does turning off the Automatic Mock Repository Cleanup Enabled flag in the JustMock options.

I've attached a simple .NET 6.0 based project that demonstrates the issue.

1 Answer, 1 is accepted

Sort by
0
Accepted
Ivo
Telerik team
answered on 12 Oct 2022, 12:25 PM

Hello Anthony,

Thank you for reporting this issue, more likely it is caused by the instrumentation done by the JusMock profiler. This problem requires a detailed low-level investigation that takes time to be performed. Until we have more info, I would recommend using a classic version of the statement as a workaround. Thanks for understanding and stay tuned for updates.

Regards,
Ivo
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Ivo
Telerik team
commented on 17 Oct 2022, 12:36 PM

A quick investigation of the issue confirms the initial assumption about the root cause and requires a code change in order to be fixed. For the purpose of further activities on this topic, I have created an item in our Feedback portal. In appreciation of your involvement, I have granted some Telerik points.
Tags
General Discussions
Asked by
Anthony
Top achievements
Rank 1
Answers by
Ivo
Telerik team
Share this question
or