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.