You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
* 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.
Copy file name to clipboardExpand all lines: README.md
+5-2Lines changed: 5 additions & 2 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -27,7 +27,7 @@ Design rules used to make your unit tests more strongly strict.
27
27
| Rule | Description |
28
28
| - | - |
29
29
|[PosInfoMoq1000: `VerifyAll()` methods should be called when instantiate a `Mock<T>` instances](docs/Design/PosInfoMoq1000.md)| When instantiating a `Mock<T>` in the *Arrange* phase of an unit test, `VerifyAll()` method should be called in the *Assert* phase to check the setup methods has been called. |
30
-
|[PosInfoMoq1001: The `Mock<T>` instance behavior should be defined to Strict mode](docs/Design/PosInfoMoq1001.md)| When instantiating a `Mock<T>` instance, the `MockBehavior` of the `Mock` instance should be defined to `Strict`. |
30
+
|[PosInfoMoq1001: The mocked instances behaviors should be defined to `Strict` mode](docs/Design/PosInfoMoq1001.md)| When instantiating a `Mock<T>` instance, the `MockBehavior` of the `Mock` instance should be defined to `Strict`. |
31
31
|[PosInfoMoq1002: `Verify()` methods should be called when `Verifiable()` has been setup](docs/Design/PosInfoMoq1002.md)| When a mocked member has been setup with the `Verifiable()` method, the `Verify()` method must be called at the end of the unit test. |
32
32
|[PosInfoMoq1003: The `Callback()` method should be used to check the parameters when mocking a method with `It.IsAny<T>()` arguments](docs/Design/PosInfoMoq1003.md)| When a mocked method contains a `It.IsAny<T>()` argument, the related parameter should be checked in the `Callback()` method. |
33
33
|[PosInfoMoq1004: The `Callback()` parameter should not be ignored if it has been setup as an `It.IsAny<T>()` argument](docs/Design/PosInfoMoq1004.md)| When a mocked method contains a `It.IsAny<T>()` argument, the related parameter should not be ignored in the `Callback()` method. |
@@ -41,13 +41,16 @@ All the rules of this category should not be disabled (or changed their severity
41
41
| - | - |
42
42
|[PosInfoMoq2000: The `Returns()` or `ReturnsAsync()` methods must be call for Strict mocks](docs/Compilation/PosInfoMoq2000.md)| When a `Mock<T>` has been defined with the `Strict` behavior, the `Returns()` or `ReturnsAsync()` method must be called when setup a method to mock which returns a value. |
43
43
|[PosInfoMoq2001: The `Setup()` method must be used only on overridable members](docs/Compilation/PosInfoMoq2001.md)) | The `Setup()` method must be applied only for overridable members. |
44
-
|[PosInfoMoq2002: `Mock<T>` class can be used only to mock non-sealed class](docs/Compilation/PosInfoMoq2002.md)| The `Mock<T>` can mock only interfaces or non-`sealed` classes. |
44
+
|[PosInfoMoq2002: `Mock<T>` class can be used only to mock non-sealed class](docs/Compilation/PosInfoMoq2002.md)| The `Mock<T>`class can mock only interfaces or non-`sealed` classes. |
45
45
|[PosInfoMoq2003: The `Callback()` delegate expression must match the signature of the mocked method](docs/Compilation/PosInfoMoq2003.md)| The delegate in the argument of the `Callback()` method must match the signature of the mocked method. |
46
46
|[PosInfoMoq2004: Constructor arguments cannot be passed for interface mocks](docs/Compilation/PosInfoMoq2004.md)| No arguments can be passed to a mocked interface. |
47
47
|[PosInfoMoq2005: Constructor arguments must match the constructors of the mocked class](docs/Compilation/PosInfoMoq2005.md)| When instantiating a `Mock<T>`, the parameters must match one of the constructors of the mocked type. |
48
48
|[PosInfoMoq2006: The Protected().Setup() method must be use with overridable protected or internal methods](docs/Compilation/PosInfoMoq2006.md)| When using the `Protected().Setup()` configuration, the method mocked must be overridable and protected or internal. |
49
49
|[PosInfoMoq2007: The `As<T>()` method can be used only with interfaces.](docs/Compilation/PosInfoMoq2007.md)| The `As<T>()` can only be use with the interfaces. |
50
50
|[PosInfoMoq2008: The `Verify()` method must be used only on overridable members](docs/Compilation/PosInfoMoq2008.md)) | The `Verify()` method must be applied only for overridable members. |
51
+
|[PosInfoMoq2009: `Mock.Of<T>` method must be used only to mock non-sealed class](docs/Compilation/PosInfoMoq2009.md)| The `Mock.Of<T>` method can mock only interfaces or non-`sealed` classes |
52
+
|[PosInfoMoq2010: `Mock.Of<T>` method must be used only with types that contains parameterless contructor](docs/Compilation/PosInfoMoq2010.md)| The `Mock.Of<T>` method requires a non-private parameterless contructor |
53
+
|[PosInfoMoq2011: Constructor of the mocked class must be accessible.](docs/Compilation/PosInfoMoq2011.md)| The constructor of the instantiate mocked class must non-private. |
|**Title**| The `Mock<T>` instance behavior should be defined to Strict mode |
6
+
|**Title**| The mocked instances behavior should be defined to `Strict` mode |
7
7
|**Category**| Design |
8
8
|**Default severity**| Warning |
9
9
10
10
## Cause
11
11
12
-
A Mock<T> instance has been created with the `Loose` behavior instead of `Strict`.
12
+
A mocked instance has been created with the `Loose` or `Default` behavior instead of `Strict`.
13
13
14
14
## Rule description
15
15
16
-
When instantiating a `Mock<T>` instance, the `MockBehavior` of the `Mock` instance should be defined to `Strict`.
16
+
When instantiating a `Mock<T>` instance (or using the `Mock.Of<T>()` alternative), the `MockBehavior` of the `Mock` instance should be defined to `Strict`.
17
17
18
18
By default, [Moq](https://github.com/devlooped/moq) build instances of mocked instance which have a `Loose` behavior for non-setup methods.
19
19
@@ -78,9 +78,30 @@ public void GetDataFromRepository()
78
78
}
79
79
```
80
80
81
+
The same behavior should be defined if the `Mock.Of<T>()` alternative is used:
// /!\ No methods on the IRepository has been setup !
90
+
91
+
varservice=newCustomerService(repository);
92
+
93
+
// Act
94
+
varresult=service.GetDataFromRepository(); // A "MoqException" will be raised to indicate that the GetData() method has not been setup !
95
+
}
96
+
```
97
+
81
98
## How to fix violations
82
99
83
-
To fix a violation of this rule, set the `MockBehavior` to `Strict` in the constructor of the `Mock<T>` class.
100
+
To fix a violation of this rule, set the `MockBehavior` to `Strict` in the constructor of the `Mock<T>` class or as the last argument of `Mock.Of<T>()` method.
101
+
102
+
### Visual Studio fixer
103
+
A Visual Studio fixer exists to set explicitely the `MockBehavior` to `Strict` in the current document, project or solution.
104
+

0 commit comments