-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Renames the NoSealedClassAnalyzer to MockClassCanBeUsedOnlyToMockNonSealedClassAnalyzer. * Add the PosInfoMoq2009 rule. * Updates the PosInfoMoq1001 rule to check the Mock.Of<T>() usage (fixes #27). * Change the version number to 1.9.1 * Renames the MockOfCanBeUsedOnlyToMockNonSealedClassAnalyzer to MockOfAnalyzer. * Add the PosInfoMoq2010 rule to check that types for Mock.Of<T>() have parameterless contructor. * Add the PosInfoMoq2011 rule. * Fix the PosInfoMoq2005 rule to check only the class types.
- Loading branch information
1 parent
282546d
commit d639007
Showing
26 changed files
with
1,696 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -396,3 +396,6 @@ FodyWeavers.xsd | |
|
||
# JetBrains Rider | ||
*.sln.iml | ||
|
||
# Specific | ||
/tests/Moq.Analyzers.Sandbox/Sandbox.cs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# PosInfoMoq2009: `Mock.Of<T>` method must be used only to mock non-sealed class | ||
|
||
| Property | Value | | ||
|-------------------------------------|---------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2009 | | ||
| **Title** | `Mock.Of<T>` method must be used only to mock non-sealed class | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
The `Mock.Of<T>` method must be used only to mock non-sealed class. | ||
|
||
## Rule description | ||
|
||
The `Mock.Of<T>` method must be use only on the interfaces or non-`sealed` classes. | ||
|
||
For example, the following code can not mock the `Service` class because it is `sealed`. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service = Mock.Of<Service>(s => s.Property == 1234); // The Service can not be mocked, because it is a sealed class. | ||
} | ||
|
||
public class Service | ||
{ | ||
public virtual int Property { get; } | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, be sure to mock interfaces or non-sealed classes. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `MoqException` | ||
thrown with the *"Type to mock (xxx) must be an interface, a delegate, or a non-selead, non-static class"* message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
# PosInfoMoq2010: `Mock.Of<T>` method must be used only with types that contains parameterless contructor | ||
|
||
| Property | Value | | ||
|-------------------------------------|---------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2010 | | ||
| **Title** | `Mock.Of<T>` method must be used only with types that contains parameterless contructor. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
The `Mock.Of<T>` method must be used only with types that contains accessible parameterless constructor. | ||
|
||
## Rule description | ||
|
||
The `Mock.Of<T>` method must be use only for non-`sealed` classes which contains accessible parameterless constructor. | ||
|
||
For example, the following code can not mock the `Service` class because it does not contain a parameterless constructor. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service = Mock.Of<Service>(s => s.Property == 1234); // The Service can not be mocked, because not parameterless constructor exists. | ||
} | ||
|
||
public class Service | ||
{ | ||
public Service(int timeout) | ||
{ | ||
} | ||
|
||
public virtual int Property { get; } | ||
} | ||
``` | ||
|
||
In this other example, the `Service` class cannot be mocked too because it contains a private constructor. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service = Mock.Of<Service>(); // The Service can not be mocked, because the parameterless constructor is private. | ||
} | ||
|
||
public class Service | ||
{ | ||
private Service() | ||
{ | ||
} | ||
|
||
public virtual int Property { get; } | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, be sure to the mocked type contains an accessible parameterless constructor | ||
(`public`, `protected` or `internal`) | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `ArgumentException` | ||
thrown with the *"Can not instantiate proxy of class: xxx. | ||
Could not find a parameterless constructor. (Parameter 'constructorArguments')"* message. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
# PosInfoMoq2011: Constructor of the mocked class must be accessible. | ||
|
||
| Property | Value | | ||
|-------------------------------------|-------------------------------------------------------------------------| | ||
| **Rule ID** | PosInfoMoq2011 | | ||
| **Title** | Constructor of the mocked class must be accessible. | | ||
| **Category** | Compilation | | ||
| **Default severity** | Error | | ||
|
||
## Cause | ||
|
||
Constructor of the mocked class must be accessible (`public`, `protected` or `internal`) | ||
|
||
## Rule description | ||
|
||
The mocked class must not contain an inaccessible constructor. | ||
|
||
```csharp | ||
[Fact] | ||
public void Test() | ||
{ | ||
var service1 = new Mock<Service>("Hello"); // The constructor invoked is private. | ||
var service2 = new Mock<Service>("Argument 1", 2); // OK | ||
var service3 = new Mock<Service>(MockBehavior.Strict, "Argument 1", 2); // OK | ||
} | ||
|
||
public abstract class Service | ||
{ | ||
private Service(string a) | ||
{ | ||
} | ||
|
||
public Service(string a, int b) | ||
{ | ||
} | ||
} | ||
``` | ||
|
||
## How to fix violations | ||
|
||
To fix a violation of this rule, be sure to call a non-private constructor when instantiate a mocked class. | ||
|
||
## When to suppress warnings | ||
|
||
Do not suppress an error from this rule. If bypassed, the execution of the unit test will be failed with a `NotSupportedException` | ||
thrown with the *"Parent does not have a default constructor. The default constructor must be explicitly defined."* message. |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.