본문으로 건너뛰기
Skillpedia
테스트5.8k · #14630

generate-testability-wrappers

하는 일

이 스킬은 시스템 시계나 파일 접근처럼 테스트하기 어려운 정적 의존성을 위한 래퍼 인터페이스와 등록 코드를 만듭니다. 의존성을 주입하고 가짜로 테스트하려는 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓C# 클래스가 시계, 파일, 환경을 직접 호출해서 테스트하기 어려울 때.
  • ✓기존 프로젝트에 TimeProvider나 System.IO.Abstractions를 도입하고 있을 때.
  • ✓정적 API의 시그니처는 바꿀 수 없고 테스트를 위한 연결점이 필요할 때.

스킬 링크

https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/generate-testability-wrappers/SKILL.md

복사해서 AI(Claude, ChatGPT, Cursor)에 붙여넣고 지침을 따르도록 요청하세요.

사용 방법

가장 쉬운 방법입니다. 메시지를 복사해 AI에 붙여넣으세요. AI가 스킬을 읽고 지침을 따릅니다.

바로 붙여넣을 수 있는 메시지

Use this skill to help me: https://github.com/dotnet/skills/blob/main/plugins/dotnet-test/skills/generate-testability-wrappers/SKILL.md Read the SKILL.md file at that link and follow its instructions for my task.
AI가 이런 스킬을 알아서 찾아 쓰길 원하시나요?무료 Skillpedia MCP를 설치하세요. 한 줄이면 작업마다 스킬 15,000+개 중에서 검색합니다.1분 만에 설치 →

AI에게 요청하는 예시

“제 C# 클래스가 DateTime.Now와 File.ReadAllText를 직접 호출합니다. 가짜 구현으로 테스트할 수 있도록 감싸 주세요.”

매주 새 스킬 5개. 읽는 데 1분.

작동 방식

에이전트가 이미 있는 내장 추상화를 고르고, 없으면 최소 래퍼를 만들며, 클래스가 생성자를 통해 그것을 받도록 등록합니다.

기술 내용 (원본 SKILL.md)

작성자의 영어 원문을 GitHub에서 그대로 가져왔습니다.

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-dependencies and 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+) or System.IO.Abstractions
  • When creating a custom wrapper for Environment.*, Console.*, or Process.*
  • 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.

  1. Register in DI:
builder.Services.AddSingleton(TimeProvider.System);
  1. Inject into classes:
public class OrderProcessor(TimeProvider timeProvider)
{
    public bool IsExpired(Order order)
        => timeProvider.GetUtcNow() > order.ExpiresAt;
}
  1. 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

자주 묻는 질문

이미 주입된 것도 감싸나요?

아니요. 코드가 이미 주입된 인터페이스나 내장 추상화를 쓰면 새 래퍼를 추가하지 않습니다.

먼저 정적 호출을 찾아야 하나요?

네. 가장 흔한 정적 호출이 무엇인지 먼저 보여 주는 탐지 단계를 먼저 실행하면 도움이 됩니다.

모든 호출 지점을 바꾸나요?

아니요. 호출 지점을 일괄 교체하는 것은 별도의 마이그레이션 스킬이 다룹니다.

상세 정보

작성자의 원본 설명

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

인기도

GitHub 스타
5.8k
순위
#14630
라이선스
MIT
업데이트
2026. 11. 2.

이 스킬이 유용한가요?

0

로그인 투표하려면 로그인하세요.

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬