exp-mock-usage-analysis
Fungsinya
Skill ini memeriksa pengujian .NET untuk melihat pengaturan mock mana yang benar-benar dipakai oleh kode yang diuji. Skill ini untuk pengembang yang rangkaian pengujiannya punya pengaturan mock berat atau membingungkan.
Kapan dipakai
- βSaat Anda ingin menemukan pengaturan mock yang tidak terpakai atau tidak perlu dalam pengujian .NET.
- βSaat pengaturan pengujian terasa berat dan Anda ingin menyederhanakannya.
- βSaat Anda bertanya-tanya apakah mock untuk tipe logging atau options benar-benar diperlukan.
Link skill
https://github.com/dotnet/skills/blob/main/plugins/dotnet-experimental/skills/exp-mock-usage-analysis/SKILL.mdSalin lalu tempelkan ke AI Anda (Claude, ChatGPT, atau Cursor) dan minta AI mengikuti instruksinya.
Cara pakai
Cara paling mudah: salin pesan ini dan tempelkan ke AI Anda. AI akan membaca skill dan mengikuti instruksinya.
Pesan siap tempel
Untuk Claude Code, Codex, atau Cursor: jalankan di folder proyek, lalu skill langsung terpasang.
Unduh SKILL.md lalu taruh di folder skill agen Anda.
Contoh permintaan ke AI
βAudit mock di pengujian layanan pesanan saya dan beri tahu pengaturan mana yang tidak pernah dipakai kode.β
5 skill baru setiap minggu. 1 menit baca.
Cara kerjanya
Agent membaca pengujian dan kode produksi, menelusuri setiap pengaturan mock melalui jalur eksekusi kode, lalu melaporkan mana yang mati, berlebihan, atau bisa diganti.
Konten teknis (SKILL.md asli)
Teks asli dari pembuat, dalam bahasa Inggris, langsung dari 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
Pertanyaan umum
Apakah skill ini menulis pengujian baru?
Tidak. Skill ini mengaudit mock yang sudah ada dan menyarankan pengaturan yang lebih sederhana.
Apakah skill ini membutuhkan kode produksi?
Ya. Skill ini harus membaca kode yang diuji untuk tahu mock mana yang tercapai.
Pustaka mock apa saja yang dikenali?
Skill ini mengenali pustaka mock .NET yang umum, seperti Moq, NSubstitute, dan FakeItEasy.
Detail
Deskripsi asli dari pembuat
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
Popularitas
- Bintang GitHub
- 5.8k
- Peringkat
- #14580
- Lisensi
- MIT
- Diperbarui
- 2/11/2026
Komentar
- β¦
Skill lain dari repositori ini
create-custom-agent
5.8kMembantu agent Anda membuat file agen kustom VS Code dengan alatnya sendiri, instruksi, dan serah terima ke agent lain.
create-skill
5.8kMembantu agent Anda membuat skill agent baru dengan nama yang valid, deskripsi pemicu yang jelas, dan file SKILL.md yang mengikuti standar.
csharp-scripts
5.8kMembantu agent Anda menjalankan program C# kecil sebagai satu file dengan alat baris perintah .NET, tanpa membuat proyek lengkap.
dotnet-pinvoke
5.8kMembantu agent Anda memanggil pustaka native C atau C++ dari .NET dengan benar, mencakup tanda tangan, penanganan string, memori, dan safe handle.
Skill serupa
test-driven-development
311kMengajari agent Anda menulis tes yang gagal dulu, melihatnya gagal, lalu menulis kode terkecil yang lulus, sebelum membangun fitur atau perbaikan apa pun.
verification-before-completion
311kMengajari agent Anda menjalankan pemeriksaan nyata dan membaca keluarannya sebelum menyatakan tugas, perbaikan, atau build sudah selesai atau lulus.
migrate-to-shoehorn
310kMengganti type cast berisiko di file tes Anda dengan pustaka shoehorn, sehingga tes bisa memakai data parsial dan tetap lulus pemeriksaan tipe.