Mock Usage Analysis
Trace each mock setup through the production code's execution path to determine which setups are actually exercised at runtime and which are dead, unreachable, redundant, or replaceable with real implementations.
When to Use
- User asks to audit, review, or analyze mock usage in .NET tests
- User wants to find unused, unnecessary, or redundant mock setups
- User wants to simplify test setup or reduce over-mocking
- User asks whether mocks of ILogger, IOptions, or similar types are needed
When Not to Use
- User wants to write new mocks or tests (general testing guidance)
- User wants to detect non-mock test anti-patterns (use
test-anti-patterns)
- User wants to migrate between mock frameworks (out of scope)
Inputs
| Input |
Required |
Description |
| Test code |
Yes |
Test files to analyze |
| Production code |
Yes |
Code under test — essential for tracing execution paths |
Workflow
Step 1: Read all provided code
Read the test files and always read the production code. You cannot determine whether a mock setup is necessary without understanding the production method's control flow.
Identify the mock framework by scanning for its patterns:
- Moq:
new Mock<T>(), .Setup(...), .Verify(...)
- NSubstitute:
Substitute.For<T>(), .Returns(...), .Received(...)
- FakeItEasy:
A.Fake<T>(), A.CallTo(...), .MustHaveHappened()
Use the correct framework's terminology throughout your analysis.
Step 2: Trace each mock setup through the production code
For each test method, do the following:
- Identify every mock setup line (
.Setup, .Returns, A.CallTo, etc.)
- Read the production method being tested and trace its execution path for the specific inputs used in that test
- Determine which mock setups are actually reached during execution
- Classify each setup:
| Classification |
Meaning |
Example |
| Used |
The production code calls this mock during the test's execution path |
GetStock setup when Reserve is called and stock is sufficient |
| Unreachable |
The production code returns early, throws, or branches away before reaching this mock call |
UpdateStock setup when the test expects the method to throw ArgumentOutOfRangeException on the first line |
| Unused |
The mock method is never called by the production method under test at all, regardless of inputs |
GetLowStockProducts setup when testing Reserve, which never calls that method |
| Redundant |
Identical mock configurations are duplicated across multiple tests instead of being shared |
Five tests each creating new Mock<IPaymentGateway>() with the same default setup |
Pay special attention to:
- Early returns and guard clauses — setups for mocks called after a guard clause are unreachable when the guard triggers
- Exception throws — if the method throws before using dependencies, all setups for those dependencies are unnecessary
- Branch-specific logic — if a method dispatches by channel/type, setups for other channels are unused
- Verify-only tests — tests that only call
.Verify/.Received/.MustHaveHappened without asserting on the method's return value
Step 3: Check for replaceable mocks
Flag mocks of stable framework types that should use real implementations:
Mock<ILogger<T>> → NullLogger<T>.Instance (unless log output is asserted)
Mock<IOptions<T>> → Options.Create(new T { ... })
- Mocks of DTOs, records, or value objects → use
new T { ... } directly
Explicitly confirm which mocks are correctly placed — external boundaries (databases, HTTP clients, message queues, third-party APIs) and security-sensitive types should remain mocked.
Step 4: Report findings
For each finding, state:
- The specific test method and mock setup line
- Why the setup is unnecessary (trace the production code path to explain)
- A concrete fix — which lines to remove, what to replace them with, or how to extract shared setup
When multiple tests duplicate mock configurations, provide a before/after example showing how to extract shared setup into a fixture or helper method.
Validation
Common Pitfalls
| Pitfall |
Solution |
| Analyzing test code without reading production code |
Always read the production method to trace which mocks are actually called |
| Flagging mocks for external boundaries (HTTP, DB) |
These are valid isolation boundaries — keep them mocked |
Flagging ILogger mock when log output is asserted |
Only flag when the mock is set up but log output is never verified |
| Using wrong framework terminology |
Match the framework in the code: Moq (Setup/Verify), NSubstitute (Returns/Received), FakeItEasy (A.CallTo/MustHaveHappened) |
1---2name: exp-mock-usage-analysis3description: "Audits .NET test mock usage by tracing each mock setup through the production code's execution path to find dead, unreachable, redundant, or replaceable mocks. Use when the user asks to audit mock usage, find unused or unnecessary mock setups, check if mocks are needed, reduce mock duplication or over-mocking, simplify test setup, or review whether mock configurations like ILogger/IOptions should use real implementations instead. Supports Moq, NSubstitute, and FakeItEasy."4license: MIT5metadata:6 source: https://skillmd.com/skills/dotnet/exp-mock-usage-analysis7---89# Mock Usage Analysis1011Trace each mock setup through the production code's execution path to determine which setups are actually exercised at runtime and which are dead, unreachable, redundant, or replaceable with real implementations.1213## When to Use1415- User asks to audit, review, or analyze mock usage in .NET tests16- User wants to find unused, unnecessary, or redundant mock setups17- User wants to simplify test setup or reduce over-mocking18- User asks whether mocks of ILogger, IOptions, or similar types are needed1920## When Not to Use2122- User wants to write new mocks or tests (general testing guidance)23- User wants to detect non-mock test anti-patterns (use `test-anti-patterns`)24- User wants to migrate between mock frameworks (out of scope)2526## Inputs2728| Input | Required | Description |29|-------|----------|-------------|30| Test code | Yes | Test files to analyze |31| Production code | Yes | Code under test — essential for tracing execution paths |3233## Workflow3435### Step 1: Read all provided code3637Read the test files and **always** read the production code. You cannot determine whether a mock setup is necessary without understanding the production method's control flow.3839Identify the mock framework by scanning for its patterns:40- **Moq**: `new Mock<T>()`, `.Setup(...)`, `.Verify(...)`41- **NSubstitute**: `Substitute.For<T>()`, `.Returns(...)`, `.Received(...)`42- **FakeItEasy**: `A.Fake<T>()`, `A.CallTo(...)`, `.MustHaveHappened()`4344Use the correct framework's terminology throughout your analysis.4546### Step 2: Trace each mock setup through the production code4748For **each test method**, do the following:49501. Identify every mock setup line (`.Setup`, `.Returns`, `A.CallTo`, etc.)512. Read the production method being tested and trace its execution path for the specific inputs used in that test523. Determine which mock setups are actually reached during execution534. Classify each setup:5455| Classification | Meaning | Example |56|---------------|---------|---------|57| **Used** | The production code calls this mock during the test's execution path | `GetStock` setup when `Reserve` is called and stock is sufficient |58| **Unreachable** | The production code returns early, throws, or branches away before reaching this mock call | `UpdateStock` setup when the test expects the method to throw `ArgumentOutOfRangeException` on the first line |59| **Unused** | The mock method is never called by the production method under test at all, regardless of inputs | `GetLowStockProducts` setup when testing `Reserve`, which never calls that method |60| **Redundant** | Identical mock configurations are duplicated across multiple tests instead of being shared | Five tests each creating `new Mock<IPaymentGateway>()` with the same default setup |6162Pay special attention to:63- **Early returns and guard clauses** — setups for mocks called after a guard clause are unreachable when the guard triggers64- **Exception throws** — if the method throws before using dependencies, all setups for those dependencies are unnecessary65- **Branch-specific logic** — if a method dispatches by channel/type, setups for other channels are unused66- **Verify-only tests** — tests that only call `.Verify`/`.Received`/`.MustHaveHappened` without asserting on the method's return value6768### Step 3: Check for replaceable mocks6970Flag mocks of stable framework types that should use real implementations:71- `Mock<ILogger<T>>` → `NullLogger<T>.Instance` (unless log output is asserted)72- `Mock<IOptions<T>>` → `Options.Create(new T { ... })`73- Mocks of DTOs, records, or value objects → use `new T { ... }` directly7475Explicitly confirm which mocks are **correctly placed** — external boundaries (databases, HTTP clients, message queues, third-party APIs) and security-sensitive types should remain mocked.7677### Step 4: Report findings7879For each finding, state:801. The specific test method and mock setup line812. Why the setup is unnecessary (trace the production code path to explain)823. A concrete fix — which lines to remove, what to replace them with, or how to extract shared setup8384When multiple tests duplicate mock configurations, provide a before/after example showing how to extract shared setup into a fixture or helper method.8586## Validation8788- [ ] Production code was read and execution paths were traced (not just test code reviewed)89- [ ] Every finding references a specific test method and setup line90- [ ] Unreachable setups include an explanation of which production code path makes them unreachable91- [ ] Correctly-placed mocks (external boundaries) are explicitly noted as appropriate92- [ ] Correct framework terminology is used throughout (not mixing Moq/NSubstitute/FakeItEasy terms)9394## Common Pitfalls9596| Pitfall | Solution |97|---------|----------|98| Analyzing test code without reading production code | Always read the production method to trace which mocks are actually called |99| Flagging mocks for external boundaries (HTTP, DB) | These are valid isolation boundaries — keep them mocked |100| Flagging `ILogger` mock when log output is asserted | Only flag when the mock is set up but log output is never verified |101| Using wrong framework terminology | Match the framework in the code: Moq (`Setup`/`Verify`), NSubstitute (`Returns`/`Received`), FakeItEasy (`A.CallTo`/`MustHaveHappened`) |