rjmurillo/moq.analyzers

View on GitHub
docs/rules/Moq1000.md

Summary

Maintainability
Test Coverage
# Moq1000: Sealed classes cannot be mocked

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

Mocking requires generating a subclass of the class to be mocked. Sealed classes cannot be subclassed. To fix:

- Introduce an interface and mock that instead
- Use the real class and not a mock
- Unseal the class

## Examples of patterns that are flagged by this analyzer

```csharp
sealed class MyClass { }

var mock = new Mock<MyClass>(); // Moq1000: Sealed classes cannot be mocked
```

## Solution

```csharp
class MyClass { }

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

## 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 Moq1000
var mock = new Mock<MyClass>(); // Moq1000: Sealed classes cannot be mocked
#pragma warning restore Moq1000
```

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

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