generate-testability-wrappers
Skill này làm gì
Skill này tạo interface bọc và mã đăng ký cho các phụ thuộc tĩnh khó kiểm thử, chẳng hạn đồng hồ hệ thống hoặc truy cập tệp. Dành cho lập trình viên muốn đưa các phụ thuộc này vào và kiểm thử bằng bản giả.
Khi nào nên dùng
- ✓Khi một lớp C# gọi trực tiếp đồng hồ, tệp hoặc biến môi trường và bạn muốn làm cho nó có thể kiểm thử.
- ✓Khi bạn đang áp dụng TimeProvider hoặc System.IO.Abstractions trong một dự án hiện có.
- ✓Khi một API tĩnh không thể thay đổi chữ ký và cần một điểm nối để kiểm thử.
Liên kết skill
https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/generate-testability-wrappers/SKILL.mdSao chép rồi dán vào AI của bạn (Claude, ChatGPT hoặc Cursor) và nhờ nó làm theo hướng dẫn.
Cách dùng
Cách dễ nhất: sao chép tin nhắn và dán vào AI của bạn. AI sẽ đọc skill và làm theo hướng dẫn.
Tin nhắn có sẵn để dán
Dành cho Claude Code, Codex hoặc Cursor: chạy lệnh trong thư mục dự án của bạn và skill sẽ được cài đặt.
Tải tệp SKILL.md về và thả vào thư mục skill của agent.
Ví dụ cách nhờ AI
“Lớp C# của tôi gọi trực tiếp DateTime.Now và File.ReadAllText. Hãy bọc những thứ đó để tôi kiểm thử được bằng bản giả.”
5 skill mới mỗi tuần. Đọc chỉ 1 phút.
Cách hoạt động
Agent chọn abstraction có sẵn của .NET nếu có, nếu không thì tạo một wrapper tối giản và đăng ký nó để lớp nhận được qua constructor.
Nội dung kỹ thuật (SKILL.md gốc)
Lấy nguyên văn từ GitHub, do tác giả viết bằng tiếng Anh.
Generate Testability Wrappers
Generate wrapper interfaces, default implementations, and DI service registration code for untestable static dependencies. For statics that already have .NET built-in abstractions (TimeProvider, IHttpClientFactory), guide adoption of the built-in. For statics without built-in alternatives, generate custom minimal wrappers.
When to Use
- After running
detect-static-dependenciesand identifying which statics to wrap - When the user asks to make a class testable by replacing statics with injected abstractions
- When adopting
TimeProvider(.NET 8+) orSystem.IO.Abstractions - When creating a custom wrapper for
Environment.*,Console.*, orProcess.* - When a released static API needs an ambient seam because signatures cannot change
When Not to Use
- The user wants to find statics first (use
detect-static-dependencies) - The user wants to bulk-replace call sites (use
migrate-static-to-wrapper) - The static is already behind an interface
If the target already consumes an injected interface or built-in abstraction, stop: do not add a second wrapper, project, registration, or test around that seam.
A missing DI package does not by itself force an ambient seam. For an instantiable class, prefer constructor injection and compose it explicitly or show the requested registration. Use Step 5 when the API is static and its signatures must stay static, or when the user explicitly forbids caller construction/DI changes.
Inputs
| Input | Required | Description |
|---|---|---|
| Static category | Yes | Which category: time, filesystem, environment, network, console, process |
| Target framework | Yes | The TargetFramework from .csproj (affects which built-in abstractions exist) |
| Composition | No | Existing DI framework, explicit/manual construction, or immutable static API |
| Namespace | No | Target namespace for generated wrapper code |
Workflow
Step 1: Determine the abstraction strategy
Based on the category and target framework:
| Category | .NET 8+ | .NET 6-7 | .NET Framework |
|---|---|---|---|
| Time | TimeProvider (built-in) |
TimeProvider via Microsoft.Bcl.TimeProvider NuGet |
Custom ISystemClock |
| File system | System.IO.Abstractions (NuGet) |
Same | Same |
| HTTP | IHttpClientFactory (built-in) |
Same | Same |
| Environment | Custom IEnvironmentProvider |
Same | Same |
| Console | Custom IConsole |
Same | Same |
| Process | Custom IProcessRunner |
Same | Same |
The table picks which abstraction. How it reaches the code under test is a separate axis:
- instantiable class: constructor injection, even if current callers compose the object manually;
- existing container: add compile-ready registration following its conventions;
- public static API/signatures that cannot change: Step 5's ambient seam.
Check for a host builder, IServiceCollection, existing registrations, and
construction sites. Do not infer "must remain static" merely because the project
currently has no container.
Step 2: Generate built-in abstraction adoption (Time, HTTP)
TimeProvider (.NET 8+)
No wrapper code needed. Complete all four parts: production registration,
constructor injection, a FakeTimeProvider test, and the testing package.
- Register in DI:
builder.Services.AddSingleton(TimeProvider.System);
- Inject into classes:
public class OrderProcessor(TimeProvider timeProvider)
{
public bool IsExpired(Order order)
=> timeProvider.GetUtcNow() > order.ExpiresAt;
}
- Test with
FakeTimeProvider:
// Requires Microsoft.Extensions.TimeProvider.Testing NuGet
var fakeTime = new FakeTimeProvider(new DateTimeOffset(2026, 1, 15, 0, 0, 0, TimeSpan.Zero));
var processor = new OrderProcessor(fakeTime);
fakeTime.Advance(TimeSpan.FromDays(1));
Assert.True(processor.IsExpired(order));
The assertion must prove a time-dependent result after the fake is pinned or
advanced. Merely constructing FakeTimeProvider is not a test. When the project
has no container but the target is an instantiable class, inject
TimeProvider anyway and show explicit production construction with
TimeProvider.System; do not replace it with a custom static clock.
Before calling the adoption complete, verify the repository contains or the answer supplies every required artifact: the testing package reference, the production compositi
Câu hỏi thường gặp
Nó có bọc thứ đã được inject sẵn không?
Không. Nếu code đã dùng interface được inject hoặc abstraction có sẵn, sẽ không thêm wrapper mới.
Tôi có nên tìm các lời gọi tĩnh trước không?
Nên làm vậy. Chạy bước phát hiện trước sẽ cho thấy lời gọi tĩnh nào xuất hiện nhiều nhất.
Nó có thay thế mọi nơi gọi không?
Không. Việc thay thế hàng loạt các nơi gọi do một skill di chuyển riêng đảm nhận.
Chi tiết
Mô tả gốc của tác giả
DO NOT USE when the target already consumes an injected interface or built-in abstraction such as IFileSystem or TimeProvider, even if the request says "generate a wrapper"; no new wrapper is needed. Use only when C# source calls an ambient/static dependency and no injectable seam exists: first-time TimeProvider, IHttpClientFactory, or System.IO.Abstractions adoption; minimal Environment/Console/Process wrappers; IProcessRunner; DI registration; or an ambient seam that preserves a static API. Exclude static detection (detect-static-dependencies), migration to an existing/registered abstraction
repo: dotnet/skills
Độ phổ biến
- Sao GitHub
- 5.8k
- Vị trí xếp hạng
- #14630
- Giấy phép
- MIT
- Cập nhật
- 2/11/2026
Bình luận
- …
Thêm skill từ kho mã này
create-custom-agent
5.8kGiúp agent của bạn tạo file custom agent cho VS Code, với các tool, hướng dẫn và handoff riêng sang agent khác.
create-skill
5.8kGiúp agent của bạn tạo một agent skill mới với tên hợp lệ, mô tả kích hoạt rõ ràng và file SKILL.md đúng tiêu chuẩn.
csharp-scripts
5.8kGiúp agent của bạn chạy một chương trình C# nhỏ dưới dạng một file duy nhất bằng công cụ dòng lệnh .NET, không cần tạo project đầy đủ.
dotnet-pinvoke
5.8kGiúp agent của bạn gọi các thư viện native C hoặc C++ từ .NET đúng cách, gồm khai báo chữ ký, xử lý chuỗi, bộ nhớ và safe handle.
Skill tương tự
test-driven-development
311kDạy agent của bạn viết kiểm thử thất bại trước, xem nó thất bại, rồi viết đoạn mã ngắn nhất để vượt qua trước khi làm bất kỳ tính năng hay bản sửa nào.
verification-before-completion
311kDạy agent của bạn chạy kiểm tra thực tế và đọc kết quả đầu ra trước khi khẳng định một việc, bản sửa hoặc bản build đã hoàn thành hay vượt qua.
migrate-to-shoehorn
310kThay các ép kiểu rủi ro trong tệp kiểm thử bằng thư viện shoehorn, để các bài kiểm thử dùng dữ liệu một phần vẫn qua được kiểm tra kiểu.