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

Detect usages of Received like methods in Received.InOrder block #135

Closed
tpodolak opened this issue Feb 16, 2020 · 2 comments · Fixed by #136
Closed

Detect usages of Received like methods in Received.InOrder block #135

tpodolak opened this issue Feb 16, 2020 · 2 comments · Fixed by #136
Labels
enhancement New feature or request
Milestone

Comments

@tpodolak
Copy link
Member

As discussed nsubstitute/NSubstitute#604 (comment)

@dtchepak
Copy link
Member

dtchepak commented Feb 17, 2020

From review comments, suggestion for error doc:


NS5001

CheckId NS5001
Category Usage

Cause

Usage of received-like method in Received.InOrder callback.

Rule description

A violation of this rule occurs when any of the following are used inside a Received.InOrder callback:

  • Received()
  • ReceivedWithAnyArgs()
  • DidNotReceive()
  • DidNotReceiveWithAnyArgs()

Calls within Received.InOrder are already checked to ensure they were received in the expected order. Individual received-like assertions should be moved outside the Received.InOrder callback.

How to fix violations

To fix a violation of this rule, remove received-like method calls from Received.InOrder callback.

For example:

// Incorrect:
Received.InOrder(() =>
{
    sub.Received().Baz();
    sub.Received().Bar();
})

// Correct:
Received.InOrder(() =>
{
    sub.Baz();
    sub.Bar();
})

Alternatively, move any received-like methods outside of Received.InOrder block if ordering is not important:

// Incorrect:
Received.InOrder(() =>
{
    sub.Baz();
    sub.Zap();
    sub.DidNotReceive().Bar();
})

// Correct:
Received.InOrder(() =>
{
    sub.Baz();
    sub.Zap();
})
sub.DidNotReceive().Bar();

How to suppress violations

This warning can be suppressed by disabling the warning in the ruleset file for the project.
The warning can also be suppressed programmatically for an assembly:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "NS5001:Received-like method used in Received.InOrder block.", Justification = "Reviewed")]

Or for a specific code block:

#pragma warning disable NS5001 // Received-like method used in Received.InOrder block.
// the code which produces warning
#pragma warning restore NS5001 // Received-like method used in Received.InOrder block.

@dtchepak
Copy link
Member

Sorry it took me so long to get to this ☝️ . 😞

@tpodolak tpodolak added the enhancement New feature or request label Apr 8, 2020
@tpodolak tpodolak added this to the 1.0.13 milestone Apr 8, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
2 participants