Tests should include assertions, false positive for C# JustMock Assert() helper

I have code similar to this:

//Arrange
var mock = Mock.Create<IFoo>().Arrange(x=>x.DoFoo()).OccursOnce();
// Act
mock.DoFoo();
// Assert
mock.Assert();

And this method triggers the rule “Test should include assertions”.
This pattern occurs frequently in my code.
I can add the suggested attribute to the assertion code because I don’t own that code, it’s part of the JustMock library.

How do I fix this either in my configuration, code or otherwise?

Hey there.

What version of SonarQube are you using?

  • Developer Edition
  • Version 9.9.2 (build 77730)

Hello @anemitoff,

Welcome to the community!

I am not able to reproduce the issue with your code snippet, here is what I have:

using NUnit.Framework;
using Telerik.JustMock;
using Telerik.JustMock.Helpers;

[TestFixture]
public class FooTests
{
    public interface IFoo
    {
        void DoFoo();
    }

    [Test]
    public void Test1()
    {
        //Arrange
        var mock = Mock.Create<IFoo>();
        mock.Arrange(x => x.DoFoo()).OccursOnce();

        // Act
        mock.DoFoo();

        // Assert
        mock.Assert();
    }
}

The issue does not raise in my case and if I remove the mock.Assert then the issue raises as expected.

  • Could you tell me which test framework you are using? (NUnit, MSTest, xUnit…)
    • Which version?
  • Which version of JustMock are you using?

Thank you,

I’m getting the same false positive using a different mocking library (NSubstitute) and xUnit.Net:

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);
    }
}

and I get 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.

Hello @davidkeaveny,

Can you open another topic for this issue?

Thank you,

1 Like

Hi @sebastien.marichal the new topic has been opened at S2699 FP with xUnit.Net and NSubstitute - Clean Code / Report False-positive / False-negative… - Sonar Community (sonarsource.com).

Many thanks for your help.

1 Like