Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix S2699 FP: When using NSubstitute Received with quantity #5826

Closed
nesc58 opened this issue Jul 12, 2022 · 0 comments · Fixed by #5827
Closed

Fix S2699 FP: When using NSubstitute Received with quantity #5826

nesc58 opened this issue Jul 12, 2022 · 0 comments · Fixed by #5827
Assignees
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Milestone

Comments

@nesc58
Copy link
Contributor

nesc58 commented Jul 12, 2022

Description

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

The fix must be done here:

private static readonly Dictionary<string, KnownType> KnownAssertions = new Dictionary<string, KnownType>
{
{"DidNotReceive", KnownType.NSubstitute_SubstituteExtensions},
{"DidNotReceiveWithAnyArgs", KnownType.NSubstitute_SubstituteExtensions},
{"Received", KnownType.NSubstitute_SubstituteExtensions},
{"ReceivedWithAnyArgs", KnownType.NSubstitute_SubstituteExtensions},
{"InOrder", KnownType.NSubstitute_Received }
};

This should contain something like

{"Received", KnownType.NSubstitute_ReceivedExtensions_ReceivedExtensions},
{"ReceivedWithAnyArgs", KnownType.NSubstitute_ReceivedExtensions_ReceivedExtensions},

Actual behavior

The rule is triggered when Received is used with Quantity object

Related information

  • MSBuild / dotnet version: 6.0.301
  • Operating System: Windows 10
  • SonarScanner MSBuild: 5.7.1
@pavel-mikula-sonarsource pavel-mikula-sonarsource added Type: False Positive Rule IS triggered when it shouldn't be. Area: C# C# rules related issues. labels Jul 13, 2022
@pavel-mikula-sonarsource 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
@pavel-mikula-sonarsource pavel-mikula-sonarsource added this to the 8.42 milestone Jul 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: C# C# rules related issues. Type: False Positive Rule IS triggered when it shouldn't be.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants