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?
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…)
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.