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
# If you wish to specify custom queries, you can do so here or in a config file.
@@ -28,7 +28,7 @@ jobs:
28
28
# Prefix the list here with "+" to use these queries and those in the config file.
29
29
# Details on CodeQL's query packs refer to : https://docs.github.com/en/code-security/code-scanning/automatically-scanning-your-code-for-vulnerabilities-and-errors/configuring-code-scanning#using-queries-in-ql-packs
This is especially useful for codebases that already use a custom Moq wrapper to ensure disposable concrete classes execute their real `Dispose(bool)` logic.
Enabling proper disposal behavior for `Mock<T>` instances can expose issues in existing unit tests or in the classes being mocked. The following are common scenarios observed when applying this rule.
139
+
140
+
### Interaction with VerifyAll
141
+
142
+
Some tests use `mock.VerifyAll()` to ensure that all configured expectations are met. When disposal behavior is enabled (e.g., via `Protected().Setup("Dispose", ...)` or a custom disposable mock wrapper), the `Dispose()` method becomes part of the mock's observable behavior.
143
+
144
+
As a result, `Dispose()` may be expected to be called before `VerifyAll()` executes. If it is not, tests may fail with unmet expectations.
145
+
146
+
**Possible resolutions:**
147
+
- Explicitly call `Dispose()` on the mocked object before invoking `VerifyAll()`
148
+
- Relax verification (e.g., avoid `VerifyAll()` when not strictly necessary)
149
+
- Adjust mock setup to exclude `Dispose()` from strict verification scenarios
150
+
151
+
---
152
+
153
+
### Non-Idempotent or Fragile Dispose Implementations
154
+
155
+
In some cases, enabling real disposal logic can cause tests to fail due to issues in the underlying class implementation. For example:
156
+
157
+
-`Dispose()` may assume certain fields are initialized
158
+
- Cleanup logic may not be safe to call in partially constructed states
159
+
- Multiple calls to `Dispose()` may not be handled correctly
160
+
161
+
This can lead to runtime exceptions such as `NullReferenceException`.
162
+
163
+
**Possible resolutions:**
164
+
- Ensure `Dispose()` implementations are **idempotent** (safe to call multiple times)
165
+
- Add appropriate null checks or guards in disposal logic
166
+
- Review object initialization to ensure required state is established before disposal
167
+
168
+
---
169
+
170
+
### Summary
171
+
172
+
This analyzer helps ensure that mocked disposable objects behave more like their real counterparts. However, enabling disposal may reveal latent issues in tests or implementations that were previously hidden due to Moq's default behavior of not calling base methods.
173
+
174
+
These issues are typically indicative of real correctness or robustness problems and should be addressed rather than suppressed.
94
175
95
176
## Limitations
96
177
97
178
This analyzer currently focuses on direct `new Mock<T>(...)` constructions and same-member setup patterns. It may not detect disposal configuration performed indirectly through helper methods, factory abstractions, or other layers of indirection.
98
179
180
+
The current CodeFix requires `dotnet_code_quality.PH2160.preferred_disposable_mock_type` to be configured.
181
+
182
+
The current implementation uses the fully qualified configured type name directly instead of adding a `using` statement and simplifying the type name.
183
+
184
+
This analyzer does not ensure that the mocked object is actually disposed. It merely ensures that, if disposed, that the protected virtual Dispose method is accounted for. Typically, this approach is appropriate, as mocked objects are often injected into another class, and that class' Dispose will Dispose our mocked object.
// Pass the preferred disposable mock type from editorconfig as additional property, so that code fix can use it to determine which type to suggest in the code fix message and when applying the code fix.
0 commit comments