I've encountered a strange problem that appeared when running tests only in nCrunch - VS test runner and R# test runner both executed those tests successfully.
For it to run you need to install packages:
- AutoFixture.AutoNSubstitute 3.43
- xunit 2.1.0
The test:
Code:
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using NSubstitute;
using Ploeh.AutoFixture;
using Ploeh.AutoFixture.AutoNSubstitute;
using Xunit;
namespace Bug
{
public interface IServiceA
{
IServiceB GetService();
}
public interface IServiceB
{
}
public class Tests
{
[Theory]
[InlineData(1)]
[InlineData(2)]
[InlineData(32)]
[InlineData(64)]
[InlineData(128)]
public async Task Test(int dop)
{
// arrange
var fixture = new Fixture();
//fixture.Customize(new AutoNSubstituteCustomization()); // no error
fixture.Customize(new AutoConfiguredNSubstituteCustomization());
var start = new SemaphoreSlim(0, dop);
var cts = new CancellationTokenSource(TimeSpan.FromSeconds(1));
//var service_a = Substitute.For<IServiceA>(); // no error
var service_a = fixture.Freeze<IServiceA>();
var tasks = Enumerable
.Range(0, dop)
.Select(_ => Task.Run
(async () =>
{
await start.WaitAsync(cts.Token).ConfigureAwait(false);
service_a.GetService();
},
cts.Token));
// act
start.Release(dop);
await Task.WhenAll(tasks).ConfigureAwait(false);
// assert
service_a.Received(dop).GetService();
}
}
}
And those tests executed in nCrunch in completely random fail results like this:
Code:
NSubstitute.Exceptions.ReceivedCallsException: Expected to receive exactly 32 calls matching:
GetService()
Actually received 34 matching calls:
GetService()
GetService()
GetService()
...
Somehow it connected to AutoFixture auto substituing methods via NSubstitute using AutoConfiguredNSubstituteCustomization (see "no error" strings in code - uncommenting either line fixed the issue). But the problem is that, like I've said - it only works incorrectly in nCrunch.