Hi,
Previous threads have said that NSubstitute is supported by S2699, but we are seeing it raised constantly across our test projects. The code compiles normally, and the only verification in the affected tests is an NSubstitute Received() (or similar) call.
Environment:
- SonarQube Cloud, Automatic Analysis (GitHub repositories; no scanner steps in our build pipelines)
- Language: C#, .NET 10 (net10.0)
- Test framework: MSTest 4.2.1 (MSTest.TestFramework / MSTest.TestAdapter), Microsoft.NET.Test.Sdk 18.4.0
- NSubstitute: 5.3.0 (also seen with 6.2.0)
Minimal example (a test that is flagged):
using NSubstitute;
[TestClass]
public class OrderPageTests
{
private readonly IOrderWriter _orderWriterMock = Substitute.For<IOrderWriter>();
[TestMethod]
public async Task Saving_the_page_writes_the_selection()
{
var id = Guid.NewGuid();
await new OrderPage(_orderWriterMock).SaveAsync(id, CancellationToken.None);
await _orderWriterMock.Received(1).SaveSelectionAsync(id, Arg.Any<CancellationToken>());
}
}
public interface IOrderWriter
{
Task SaveSelectionAsync(Guid id, CancellationToken cancellationToken);
}
Expected: no issue, because Received(1) fails the test if the call was not made, so it is an assertion.
Actual: S2699 “Add at least one assertion to this test case.”
As we use Automatic Analysis, I suspect the NSubstitute package isn’t resolved during analysis. If so, could S2699 fall back to recognising Received / DidNotReceive / ReceivedWithAnyArgs by name when the symbol can’t be resolved, as it already does for names containing “Assert” or “Should”? Alternatively, could Automatic Analysis restore NuGet packages for test projects?
We have been marking these as false positives, but every new test of this shape raises a new issue, so this adds noise for the whole team.
Thanks