Generated banner for Exp Mock Usage Analysis
Generated logo for Exp Mock Usage Analysis

Exp Mock Usage Analysis

.NET (Microsoft)OfficialPublished by .NET (Microsoft)

  • Skill
  • Review and audit
  • Software development
  • MIT
  • No issues found

From the author

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.
— the description by .NET (Microsoft)

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill provides a systematic approach to auditing mock usage in .NET unit tests. By tracing mock setups against the actual production code execution path, it helps developers identify unreachable, unused, or redundant mocks. It supports Moq, NSubstitute, and FakeItEasy, and provides guidance on replacing mocks of stable framework types like ILogger or IOptions with real implementations, while ensuring external boundaries remain properly isolated.

What it does

  • Traces mock setups through production code execution paths.
  • Identifies dead, unreachable, and unused mock configurations.
  • Detects redundant mock setups that should be shared across tests.
  • Suggests replacing mocks of stable types with real implementations.
  • Supports Moq, NSubstitute, and FakeItEasy frameworks.
  • Provides concrete refactoring advice for test suites.

When to use it

  • When auditing or reviewing mock usage in .NET test suites.
  • When trying to reduce over-mocking and simplify complex test setups.
  • When investigating whether mocks for ILogger or IOptions are necessary.
  • When cleaning up redundant mock configurations across multiple tests.

Advantages

  • Improves test reliability by removing dead or misleading mock setups.
  • Reduces test maintenance overhead by identifying redundant configurations.
  • Encourages the use of real implementations for stable framework types.
  • Ensures accurate analysis by requiring production code context.

Usage examples

“Audit my test suite for redundant mock setups and suggest improvements.”
The agent will analyze your test and production code, identify duplicate mock configurations, and provide a refactored example using a shared fixture or helper method.
“Are these ILogger mocks in my test file actually necessary?”
The agent will trace the production code to see if the logger is verified; if not, it will suggest replacing the mock with NullLogger.
“Why is my mock setup for the database not being triggered?”
The agent will trace the execution path to determine if the code returns early or throws an exception before reaching the database call.

Install

  1. Download the skill as a ZIP:

    https://mp-staging.autogineer.com/agent-skills/dotnet/exp-mock-usage-analysis/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip exp-mock-usage-analysis.zip -d .agents/skills

Documentation by .NET (Microsoft)

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

InputRequiredDescription
Test codeYesTest files to analyze
Production codeYesCode 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:

  1. Identify every mock setup line (.Setup, .Returns, A.CallTo, etc.)

  2. Read the production method being tested and trace its execution path for the specific inputs used in that test

  3. Determine which mock setups are actually reached during execution

  4. Classify each setup:

ClassificationMeaningExample
UsedThe production code calls this mock during the test's execution pathGetStock setup when Reserve is called and stock is sufficient
UnreachableThe production code returns early, throws, or branches away before reaching this mock callUpdateStock setup when the test expects the method to throw ArgumentOutOfRangeException on the first line
UnusedThe mock method is never called by the production method under test at all, regardless of inputsGetLowStockProducts setup when testing Reserve, which never calls that method
RedundantIdentical mock configurations are duplicated across multiple tests instead of being sharedFive 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:

  1. The specific test method and mock setup line

  2. Why the setup is unnecessary (trace the production code path to explain)

  3. 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

  • [ ] Production code was read and execution paths were traced (not just test code reviewed)

  • [ ] Every finding references a specific test method and setup line

  • [ ] Unreachable setups include an explanation of which production code path makes them unreachable

  • [ ] Correctly-placed mocks (external boundaries) are explicitly noted as appropriate

  • [ ] Correct framework terminology is used throughout (not mixing Moq/NSubstitute/FakeItEasy terms)

Common Pitfalls

PitfallSolution
Analyzing test code without reading production codeAlways 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 assertedOnly flag when the mock is set up but log output is never verified
Using wrong framework terminologyMatch the framework in the code: Moq (Setup/Verify), NSubstitute (Returns/Received), FakeItEasy (A.CallTo/MustHaveHappened)

Files

Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Published by
.NET (Microsoft)
Publisher
.NET (Microsoft) (Official)
Source
dotnet/skills · plugins/dotnet-experimental/skills/exp-mock-usage-analysis · commit 96c3d68
Licence
MIT License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by .NET (Microsoft).

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download exp-mock-usage-analysis.zip