docs/rules/Moq1400.md
# Moq1400: Explicitly choose a mocking behavior instead of relying on the default (Loose) behavior
| 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.
## Examples of patterns that are flagged by this analyzer
```csharp
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
```
```csharp
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
```
## Solution
```csharp
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`
```
## Suppress a warning
If you just want to suppress a single violation, add preprocessor directives to
your source file to disable and then re-enable the rule.
```csharp
#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](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/configuration-files).
```ini
[*.{cs,vb}]
dotnet_diagnostic.Moq1400.severity = none
```
For more information, see
[How to suppress code analysis warnings](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings).