rjmurillo/moq.analyzers

View on GitHub
docs/rules/Moq1002.md

Summary

Maintainability
Test Coverage
# Moq1002: Parameters provided into mock do not match any existing constructors

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

In order to construct the mocked type, constructor parameters must match a constructor. To fix:

- Match the arguments to `Mock` with a constructor of the mocked type

## Examples of patterns that are flagged by this analyzer

```csharp
class MyClass
{
    MyClass(string s) { }
}

var mock = new Mock<MyClass>(3); // Moq1002: Parameters provided into mock do not match any existing constructors
```

## Solution

```csharp
class MyClass
{
    MyClass(string s) { }
}

var mock = new Mock<MyClass>("three");
```

## 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 Moq1002
var mock = new Mock<MyClass>(3); // Moq1002: Parameters provided into mock do not match any existing constructors
#pragma warning restore Moq1002
```

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

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