Skip to content

Latest commit

 

History

History
46 lines (34 loc) · 1.66 KB

File metadata and controls

46 lines (34 loc) · 1.66 KB

NS1004

CheckId NS1004
Category Non-substitutable member

Cause

Argument matcher used with a non-virtual member of a class.

Rule description

A violation of this rule occurs when an argument matcher such as Arg.Is or Arg.Any is used with a non-virtual member of a class. For example:

// Incorrect if `NonVirtualMethod` is not virtual:
sub.NonVirtualMethod(Arg.Any<int>()).Returns(42);

This violation can also be reported if an argument matcher is used for something other than setting return values, checking received calls, or configuring callbacks. See NSubstitute's argument matcher documentation for more information.

How to fix violations

To fix a violation of this rule, make the member of your class virtual or substitute for interface.

How to suppress violations

This warning can be suppressed by disabling the warning in the ruleset file for the project or by suppressing it (for selected members) in nsubstitute.json file. See the configuration section for details on how to set this up. The warning can also be suppressed programmatically for an assembly:

[assembly: System.Diagnostics.CodeAnalysis.SuppressMessage("Non-substitutable member", "NS1004:Arg matcher with non-virtual member", Justification = "Reviewed")]

Or for a specific code block:

#pragma warning disable NS1004 // Arg matcher with non-virtual member.
// the code which produces warning
#pragma warning restore NS1004 // Arg matcher with non-virtual member.