rjmurillo/moq.analyzers

View on GitHub
docs/rules/Moq1001.md

Summary

Maintainability
Test Coverage
# Moq1001: Mocked interfaces cannot have constructor parameters

| Item     | Value   |
| -------- | ------- |
| Enabled  | True    |
| Severity | Warning |
| CodeFix  | False   |
---

Mocking interfaces requires generating a class on-the-fly that implements the interface. That generated class is
constructed using the default constructor. To fix:

- Remove the constructor parameters

## Examples of patterns that are flagged by this analyzer

```csharp
interface IMyService
{
    void Do(string s);
}

var mock = new Mock<IMyService>("123"); // Moq1001: Mocked interfaces cannot have constructor parameters
```

## Solution

```csharp
interface IMyService
{
    void Do(string s);
}

var mock = new Mock<IMyService>();
```

## 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 Moq1001
var mock = new Mock<IMyService>("123"); // Moq1001: Mocked interfaces cannot have constructor parameters
#pragma warning restore Moq1001
```

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.Moq1001.severity = none
```

For more information, see
[How to suppress code analysis warnings](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings).