Hi,
I'm using NCrunch (latest version) to run a few hundred unit tests implemented with NUnit 3, Autofixture and the Autodata / AutoMoqData attributes. My tests always pass when running them sequentially, but when parallelizing them sometimes I get some random errors that seem to be related to multi-threading.
For instance this just failed once:
Code:
[Theory]
[AutoMoqData]
public async Task DownloadAddsAllNewEntitiesToRepository(
[Frozen] IApiCollectionProxy<Project> proxy,
[Frozen] IEntityService<Project> entityService,
List<Project> input,
ProjectsSyncManager sut)
{
//Setup
var lastSyncTime = input.Min(e => e.LastChangeTime);
var proxyMock = Mock.Get(proxy);
var cancellationToken = CancellationToken.None;
proxyMock.Setup(p => p.LoadAllAsync(It.IsAny<Expression<Func<Project, bool>>>(), cancellationToken))
.Returns((Expression<Func<Project, bool>> exp, CancellationToken can) => Task.FromResult(input.Where(exp.Compile())));
Mock.Get(entityService).Setup(s => s.Exists(It.IsAny<int>())).Returns(false);
Mock.Get(entityService).Setup(s => s.FindDuplicate(It.IsAny<Project>()))
.Returns((Project p)=>null);
//Execution
await sut.DownloadAsync(2, lastSyncTime, cancellationToken);
//Assertion
//ASSERTION IN NEXT LINE FAILED: Expected invocation on the mock exactly 3 times, but was 6 times: s => s.Add(It.IsAny<Project>())
Mock.Get(entityService).Verify(s => s.Add(It.IsAny<Project>()), Times.Exactly(input.Count()));
}
In this case it seems that the same mock is used in two different unit tests at once. If I re-run the test it does not fail. Errors like these are quite rare, but all over the place in my test suite.
Now I am not even sure which technology I am using here is at fault. Is this an NCrunch problem, an NUnit Problem, an Autofixture problem?
And is there any workaround (Short of running everything in isolation or sequentially)?
Thanks,
Adrian