rjmurillo/moq.analyzers

View on GitHub
docs/rules/Moq1101.md

Summary

Maintainability
Test Coverage
# Moq1101: SetupGet/SetupSet should be used for properties, not for methods

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

`.SetupGet()` and `.SetupSet()` are methods for mocking properties, not methods. Use `.Setup()` to mock methods instead.

## Examples of patterns that are flagged by this analyzer

```csharp
interface IMyInterface
{
    string Method();
}

var mock = new Mock<IMyInterface>().SetupGet(x => x.Method()); // Moq1101: SetupGet/SetupSet should be used for properties, not for methods
```

## Solution

```csharp
interface IMyInterface
{
    string Method();
}

var mock = new Mock<IMyInterface>().Setup(x => x.Method());
```

## 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 Moq1101
var mock = new Mock<IMyInterface>().SetupGet(x => x.Method()); // Moq1101: SetupGet/SetupSet should be used for properties, not for methods
#pragma warning restore Moq1101
```

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

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