S2699 FP with xUnit.Net and NSubstitute

I’m getting false positives for csharpsquid:S2699 when using xUnit.Net and NSubstitute with SonarCloud. My test doesn’t have any xUnit assertions, the assertions are being handled by NSubstitute instead:

public class WhenUploadingFile
{
    private readonly IFileSystem _fileSystem = Substitute.For<IFileSystem>();
    private readonly MessageHandler _sut;
    
    public WhenUploadingFile()
    {
        _sut = new MessageHandler(_fileSystem);
    }

    [Fact]
    public void ItShouldCreateTheFolder()
    {
        var path = string.Format("/{0:yyyy}/{0:MM}/{0:dd}/", DateTime.Today);
        _fileSystem.Received(1).CreateFolder(path);
    }
}

SonarCloud reports csharpsquid:S2699 on my ItShouldCreateTheFolder test; the test will fail if the substitute IFileSystem does not receive the expected call, but SonarCloud isn’t recognising that.

SonarCloud should recognise that even if no xUnit assertions are present, the NSubstitute library is verifying the correct operation of the SUT.

Hello @davidkeaveny,

I wasn’t able to re-produce the issue on my side. S2699 supports NSubstitute assertions which is also documented in our rules description. I tried the following code, which should be equivalent to yours:

    public class Test
    {
        private readonly IEqualityComparer _equalityComparer = Substitute.For<IEqualityComparer>();
        private readonly OrderedDictionary _sut;

        public Test()
        {
            _sut = new OrderedDictionary(_equalityComparer);
        }

        [Fact]
        public void ItShouldCreateTheFolder()
        {
            _sut.Add("key1", "value");
            _sut.Add("key2", "value");
            _equalityComparer.Received(1).Equals("key1", "key2");
        }
    }

Here S2699 isn’t raised. Can you provide more information about your setup?

Please give us the following version information:

  • do you use automatic analysis for SonarCloud or CI?
  • what is the version of the Scanner for .NET (MSBuild) that you are using?
  • what version of MSBuild are you using?
  • it also helps us to see have the full log:
    • please give us the verbose output of the scanner commands (please run SonarScanner.MSBuild.exe begin /k:“MyProject” /d:sonar.verbose=true as the begin step, and please attach the output of the BEGIN and END steps)
    • please give the output of running MSBuild in verbose mode (/v:d)