You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Rule S2699 is false positive triggered using NSubstitute Received method with Quanity arg.
Repro steps
public interface ITestClass
{
public void DoSomething();
}
public class Class1
{
[Test]
public void Works() // All fine
{
var substitute = Substitute.For<ITestClass>();
substitute.DoSomething();
substitute.Received(1).DoSomething();
}
[Test]
public void NotWorking() // S2699 is triggered
{
var substitute = Substitute.For<ITestClass>();
substitute.DoSomething();
substitute.DoSomething();
substitute.Received(Quantity.Exactly(1)).DoSomething();
}
}
Expected behavior
This rule should not be triggeed using NSubstitutes Received with Quantity object.
Received with int comes from NSubstitute.SubstituteExtensions
Received with quantity comes from NSubstitute.ReceivedExtensions.ReceivedExtensions
Internally ReceivedExtensions.ReceivedExtensions is used by the SubstituteExtensions.
The SubstituteExtensions provides a few shorthands substitute.Received().DoSomething(); -> substitute.Received(Quantity.AtLeastOne()).DoSomething(); substitute.Received(1).DoSomething(); -> substitute.Received(Quantity.Exactly(1)).DoSomething();
pavel-mikula-sonarsource
changed the title
S2699: False positive using NSubstitute Received with quantity
Fix S2699 FP: When using NSubstitute Received with quantity
Jul 13, 2022
Description
Rule S2699 is false positive triggered using NSubstitute Received method with Quanity arg.
Repro steps
Expected behavior
This rule should not be triggeed using NSubstitutes Received with Quantity object.
Received with int comes from
NSubstitute.SubstituteExtensions
Received with quantity comes from
NSubstitute.ReceivedExtensions.ReceivedExtensions
Internally ReceivedExtensions.ReceivedExtensions is used by the SubstituteExtensions.
The SubstituteExtensions provides a few shorthands
substitute.Received().DoSomething();
->substitute.Received(Quantity.AtLeastOne()).DoSomething();
substitute.Received(1).DoSomething();
->substitute.Received(Quantity.Exactly(1)).DoSomething();
The fix must be done here:
sonar-dotnet/analyzers/src/SonarAnalyzer.CSharp/Rules/TestMethodShouldContainAssertion.cs
Lines 42 to 49 in 90b1585
This should contain something like
Actual behavior
The rule is triggered when Received is used with Quantity object
Related information
The text was updated successfully, but these errors were encountered: