Item | Value |
---|---|
Enabled | True |
Severity | Warning |
CodeFix | False |
Mocks use the MockBehavior.Loose
by default. Some people find this default behavior undesirable, as it can lead to
unexpected behavior if the mock is improperly set up. To fix, specify either MockBehavior.Loose
or
MockBehavior.Strict
to signify acknowledgement of the mock's behavior.
interface ISample
{
int Calculate() => 0;
}
var mock = new Mock<ISample>(); // Moq1400: Moq: Explicitly choose a mock behavior
var mock2 = Mock.Of<ISample>(); // Moq1400: Moq: Explicitly choose a mock behavior
interface ISample
{
int Calculate() => 0;
}
var mock = new Mock<ISample>(MockBehavior.Default); // Moq1400: Moq: Explicitly choose a mock behavior
var mock2 = Mock.Of<ISample>(MockBehavior.Default); // Moq1400: Moq: Explicitly choose a mock behavior
var repo = new MockRepository(MockBehavior.Default); // Moq1400: Moq: Explicitly choose a mock behavior
interface ISample
{
int Calculate() => 0;
}
var mock = new Mock<ISample>(MockBehavior.Strict); // Or `MockBehavior.Loose`
var mock2 = new Mock.Of<ISample>(MockBehavior.Strict); // Or `MockBehavior.Loose`
var repo = new MockRepository(MockBehavior.Strict); // Or `MockBehavior.Loose`
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule.
#pragma warning disable Moq1400
var mock = new Mock<ISample>(); // Moq1400: Moq: Explicitly choose a mock behavior
#pragma warning restore Moq1400
To disable the rule for a file, folder, or project, set its severity to none
in the
configuration file.
[*.{cs,vb}]
dotnet_diagnostic.Moq1400.severity = none
For more information, see How to suppress code analysis warnings.