exp-mock-usage-analysis
它能做什么
这项技能检查 .NET 测试,看看哪些模拟设置真正被被测代码使用。适合测试套件中模拟设置繁重或令人困惑的开发者。
适用场景
- ✓当你想在 .NET 测试中找出未使用或不必要的模拟设置时。
- ✓当测试设置显得过于繁重,你想加以简化的时候。
- ✓当你怀疑日志或选项类型的模拟是否真的必要的时候。
技能链接
https://github.com/dotnet/skills/blob/main/plugins/dotnet-experimental/skills/exp-mock-usage-analysis/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请审计我订单服务测试中的模拟对象,并告诉我哪些设置代码从未使用。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体会阅读测试和生产代码,沿着代码的执行路径追踪每个模拟设置,然后报告哪些设置是无用的、重复的或可替换的。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
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/.MustHaveHappenedwithout 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
- 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 unreach
常见问题
它会编写新测试吗?
不会。它审计现有的模拟对象,并建议更简单的设置。
它需要生产代码吗?
需要。它必须读取被测代码,才能知道哪些模拟会被调用。
它了解哪些模拟框架?
它能识别常见的 .NET 模拟库,例如 Moq、NSubstitute 和 FakeItEasy。
详情
作者的原始描述
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.
repo: dotnet/skills
热度
- GitHub 星标
- 5.8k
- 排名
- #14580
- 许可证
- MIT
- 更新时间
- 2026/11/2
评论
- …
同一仓库的更多技能
create-custom-agent
5.8k帮你的智能体创建 VS Code 自定义智能体文件,为每个智能体设置专属工具、指令以及向其他智能体的交接。
create-skill
5.8k帮你的智能体创建新的智能体技能,包括有效的名称、清晰的触发描述,以及符合标准的 SKILL.md 文件。
csharp-scripts
5.8k帮你的智能体通过 .NET 命令行工具以单个文件运行小型 C# 程序,无需创建完整项目。
dotnet-pinvoke
5.8k帮你的智能体在 .NET 中正确调用原生 C 或 C++ 库,涵盖函数签名、字符串处理、内存和安全句柄。