Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@ dotnet add package Moq.Analyzers
> NOTE: You must use a [supported version](https://dotnet.microsoft.com/en-us/platform/support/policy/dotnet-core) of
> the .NET SDK (i.e. 6.0 or later).

### Configuring rules

Moq.Analyzers follows existing conventions for enabling, disabling, or suppressing rules. See
[Suppress code analysis warnings - .NET | Microsoft Learn](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings)
for documentation on how to configure rules for your project.

## Contributions welcome

Moq.Analyzers continues to evolve and add new features. Any help will be appreciated. You can report issues,
Expand Down
31 changes: 27 additions & 4 deletions docs/rules/Moq1000.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1000: Sealed classes cannot be mocked

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

Mocking requires generating a subclass of the class to be mocked. Sealed classes cannot be subclassed. To fix:
Expand All @@ -28,3 +28,26 @@ 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).
31 changes: 27 additions & 4 deletions docs/rules/Moq1001.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1001: Mocked interfaces cannot have constructor parameters

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

Mocking interfaces requires generating a class on-the-fly that implements the interface. That generated class is
Expand Down Expand Up @@ -33,3 +33,26 @@ interface IMyService

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).
31 changes: 27 additions & 4 deletions docs/rules/Moq1002.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1002: Parameters provided into mock do not match any existing constructors

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

In order to construct the mocked type, constructor parameters must match a constructor. To fix:
Expand Down Expand Up @@ -32,3 +32,26 @@ class MyClass

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).
33 changes: 29 additions & 4 deletions docs/rules/Moq1100.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1100: Callback signature must match the signature of the mocked method

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

The signature of the `.Callback()` method must match the signature of the `.Setup()` method. To fix:
Expand Down Expand Up @@ -37,3 +37,28 @@ 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).
31 changes: 27 additions & 4 deletions docs/rules/Moq1101.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1101: SetupGet/SetupSet should be used for properties, not for methods

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

`.SetupGet()` and `.SetupSet()` are methods for mocking properties, not methods. Use `.Setup()` to mock methods instead.
Expand All @@ -30,3 +30,26 @@ interface IMyInterface

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).
32 changes: 28 additions & 4 deletions docs/rules/Moq1200.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1200: Setup should be used only for overridable members

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

Mocking requires generating a subclass of the class to be mocked. Methods not marked `virtual` cannot be overridden.
Expand Down Expand Up @@ -34,3 +34,27 @@ class SampleClass
var mock = new Mock<SampleClass>()
.Setup(x => x.Property);
```

## 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 Moq1200
var mock = new Mock<SampleClass>()
.Setup(x => x.Property); // Moq1200: Setup should be used only for overridable members
#pragma warning restore Moq1200
```

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

For more information, see
[How to suppress code analysis warnings](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings).
32 changes: 28 additions & 4 deletions docs/rules/Moq1201.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1201: Setup of async methods should use `.ReturnsAsync` instance instead of `.Result`

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

Moq now supports the `.ReturnsAsync()` method to support mocking async methods. Use it instead of returning `.Result`,
Expand Down Expand Up @@ -33,3 +33,27 @@ class AsyncClient
var mock = new Mock<AsyncClient>()
.Setup(c => c.GetAsync()).ReturnsAsync(string.Empty);
```

## 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 Moq1201
var mock = new Mock<AsyncClient>()
.Setup(c => c.GetAsync().Result); // Moq1201: Setup of async methods should use .ReturnsAsync instance instead of .Result
#pragma warning restore Moq1201
```

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

For more information, see
[How to suppress code analysis warnings](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings).
32 changes: 28 additions & 4 deletions docs/rules/Moq1300.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1300: `Mock.As()` should take interfaces only

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

The `.As()` method is used when a mocked object must implement multiple interfaces. It cannot be used with abstract or
Expand Down Expand Up @@ -45,3 +45,27 @@ class SampleClass

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

## 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 Moq1300
var mock = new Mock<SampleClass>()
.As<SampleClass>(); // Moq1300: Mock.As() should take interfaces only
#pragma warning restore Moq1300
```

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

For more information, see
[How to suppress code analysis warnings](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/suppress-warnings).
31 changes: 27 additions & 4 deletions docs/rules/Moq1400.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
# Moq1400: Explicitly choose a mocking behavior instead of relying on the default (Loose) behavior

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

Mocks use the `MockBehavior.Loose` by default. Some people find this default behavior undesirable, as it can lead to
Expand Down Expand Up @@ -46,3 +46,26 @@ var mock = new Mock<ISample>(MockBehavior.Strict); // Or `MockBehavior.Loose`
var mock2 = new Mock.Of<ISample>(MockBehavior.Strict); // Or `MockBehavior.Loose`
var repo = new MockRepository(MockBehavior.Strict); // Or `MockBehavior.Loose`
```

## 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 Moq1400
var mock = new Mock<ISample>(); // Moq1400: Moq: Explicitly choose a mock behavior
#pragma warning restore Moq1400
```

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

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