I'm working on tests for legacy code that instantiates a ServiceController (in System.ServiceProcess) to check for a running SQL Server instance. I want to add the tests to our CI/CD build pipeline, so I decided to use JustMock to future-mock ServiceController since the service in question will not exist on the build server. These tests are all using xUnit.
However, the test (which has 3 variants specified via xUnit's InlineData attributes) started hanging on the first variant. It's not just long-running; I've let it run for hours (it's normally a 3-second test at most), and CPU usage is negligible. ServiceController is IDisposable, so I figured it might have something to do with that.
After a lot of paring down, I can repro the problem with the following code in a new project:
public class JustMockTest
{
[Theory]
[InlineData("test")]
public void TestMethod(string whatever)
{
Mock.Arrange(() => new ServiceController()).DoNothing();
}
}
However, this test (in the same class) works fine and completes in about 1s - note that it's a fact, not a theory:
[Fact]
public void TestMethod2()
{
Mock.Arrange(() => new ServiceController()).DoNothing();
}
This is JustMock v2022.1.223.1, xUnit v2.4.1.0, and VS 2022.
At first I wondered if my 3 variants were somehow stepping on each other's Mock.Arrange() statements; but as you can see, the top test, while declared as a theory, only has 1 variant and still has the problem. What's also interesting is that an analogous setup for MSTest also runs fine - so this seems to be xUnit-specific somehow:
[TestClass]
public class UnitTest1
{
[DataTestMethod]
[DataRow("test")]
[DataRow("test2")]
public void TestMethod1(string whatever)
{
Mock.Arrange(() => new ServiceController()).DoNothing();
}
}
At first I thought IDisposable classes may be the problem, but it seems it's actually MarshalByRefObject; it's a base class of ServiceController and others I can repro with (WebClient, MemoryStream, etc.), and I can also repro by mocking a simple class of my own that extends MarshalByRefObject.
Is this a known issue? Are there recommended workarounds (without having to ditch xUnit for MSTest or another framework)?
Thanks!
Jeremy