CheckId | NS1004 |
Category | Non-substitutable member |
Argument matcher used with a non-virtual member of a class.
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.
To fix a violation of this rule, make the member of your class virtual or substitute for interface.
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.