rjmurillo/moq.analyzers

View on GitHub
docs/rules/Moq1100.md

Summary

Maintainability
Test Coverage
# Moq1100: Callback signature must match the signature of the mocked method

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

The signature of the `.Callback()` method must match the signature of the `.Setup()` method. To fix:

- Ensure the parameters to `.Callback()` match the signature created by `.Setup()`. A code fix is available to automatically
  match

## Examples of patterns that are flagged by this analyzer

```csharp
interface IMyService
{
    int Do(int i, string s, DateTime dt);
}

var mock = new Mock<IMyService>()
    .Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>()))
    .Callback((string s1, int i1) => { }); // Moq1100: Callback signature must match the signature of the mocked method
```

## Solution

```csharp
interface IMyService
{
    int Do(int i, string s, DateTime dt);
}

var mock = new Mock<IMyService>()
    .Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>()))
    .Callback((int i, string s, DateTime dt) => { });
```

## 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 Moq1100
var mock = new Mock<IMyService>()
    .Setup(x => x.Do(It.IsAny<int>(), It.IsAny<string>(), It.IsAny<DateTime>()))
    .Callback((string s1, int i1) => { }); // Moq1100: Callback signature must match the signature of the mocked method
#pragma warning restore Moq1100
```

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

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