|
| 1 | +# PosInfoMoq1008: The `Mock.Verify()` and `Mock.VerifyAll()` methods must specify at least one mock |
| 2 | + |
| 3 | +| Property | Value | |
| 4 | +|---------------------|------------------------------------------------------------------------------------------| |
| 5 | +| **Rule ID** | PosInfoMoq1008 | |
| 6 | +| **Title** | The `Mock.Verify()` and `Mock.VerifyAll()` methods must specify at least one mock | |
| 7 | +| **Category** | Design | |
| 8 | +| **Default severity**| Warning | |
| 9 | + |
| 10 | +## Cause |
| 11 | + |
| 12 | +When calling the static methods `Mock.Verify()` or `Mock.VerifyAll()` without providing any `Mock<T>` instances, no verification is performed. |
| 13 | + |
| 14 | +## Rule description |
| 15 | + |
| 16 | +The static methods `Mock.Verify()` and `Mock.VerifyAll()` are designed to verify multiple `Mock<T>` instances at once. |
| 17 | +However, calling these methods without specifying any `Mock<T>` instances results in no verification being performed, |
| 18 | +which makes the test ineffective. |
| 19 | + |
| 20 | +For example, the following code calls `Mock.Verify()` without any arguments: |
| 21 | + |
| 22 | +```csharp |
| 23 | +[Fact] |
| 24 | +public void ProcessData_ShouldVerifyMocks() |
| 25 | +{ |
| 26 | + var repositoryMock = new Mock<IRepository>(); |
| 27 | + var serviceMock = new Mock<IService>(); |
| 28 | + |
| 29 | + var processor = new DataProcessor(repositoryMock.Object, serviceMock.Object); |
| 30 | + processor.ProcessData(); |
| 31 | + |
| 32 | + // This does nothing - no mocks are verified |
| 33 | + Mock.Verify(); |
| 34 | +} |
| 35 | +``` |
| 36 | + |
| 37 | +This code is ineffective because it appears to verify something, but actually performs no verification at all. |
| 38 | + |
| 39 | +Instead, you should specify the `Mock<T>` instances you want to verify: |
| 40 | + |
| 41 | +```csharp |
| 42 | +[Fact] |
| 43 | +public void ProcessData_ShouldVerifyMocks() |
| 44 | +{ |
| 45 | + var repositoryMock = new Mock<IRepository>(); |
| 46 | + var serviceMock = new Mock<IService>(); |
| 47 | + |
| 48 | + var processor = new DataProcessor(repositoryMock.Object, serviceMock.Object); |
| 49 | + processor.ProcessData(); |
| 50 | + |
| 51 | + // Clear and explicit - verifies both mocks |
| 52 | + Mock.Verify(repositoryMock, serviceMock); |
| 53 | +} |
| 54 | +``` |
| 55 | + |
| 56 | +The same applies to `Mock.VerifyAll()`: |
| 57 | + |
| 58 | +```csharp |
| 59 | +// Wrong: no verification performed |
| 60 | +Mock.VerifyAll(); |
| 61 | + |
| 62 | +// Correct: verifies all setups on the specified mocks |
| 63 | +Mock.VerifyAll(repositoryMock, serviceMock); |
| 64 | +``` |
| 65 | + |
| 66 | +## How to fix violations |
| 67 | + |
| 68 | +To fix a violation of this rule, **always provide at least one mock instance** when calling `Mock.Verify()` or `Mock.VerifyAll()`. |
| 69 | + |
| 70 | +Examples: |
| 71 | +- `Mock.Verify(mockA)` |
| 72 | +- `Mock.Verify(mockA, mockB)` |
| 73 | +- `Mock.VerifyAll(mockA, mockB, mockC)` |
| 74 | + |
| 75 | +## When to suppress warnings |
| 76 | + |
| 77 | +You should generally not suppress warnings from this rule, as calling these methods without arguments serves no purpose. |
| 78 | +If you need to verify individual mocks, use the instance methods `mock.Verify()` or `mock.VerifyAll()` instead. |
0 commit comments