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

testability-obstacle

하는 일

이 스킬은 C# 동작 하나를 테스트 가능하게 만드는 데 필요한 가장 작은 변경을 추가합니다. 예를 들어 현재 시각을 제어하거나 실제 파일 접근을 피하는 것입니다. 시스템 기능에 의존하는 코드의 결정적 테스트가 필요한 개발자를 위한 스킬입니다.

사용하는 경우

  • ✓테스트가 그대로 두면 실제 파일 시스템을 읽거나 쓸 때.
  • ✓동작이 현재 시각, 지연, 난수, 환경에 의존하며 그것을 제어해야 할 때.
  • ✓기존 테스트가 프로세스 전체 설정을 바꾸지 않고는 의존성을 제어할 수 없을 때.

스킬 링크

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

AI에게 요청하는 예시

“제 보고서 생성기 테스트가 실제 디스크에 쓰고 DateTime.Now에 의존합니다. 결정적으로 테스트할 수 있도록 가장 작은 연결점을 추가해 주세요.”

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

작동 방식

에이전트가 기존 호출은 그대로 동작하게 두면서 좁은 주입 지점을 추가하고, 그 의존성의 가짜 구현을 써서 결정적 테스트를 작성합니다.

기술 내용 (원본 SKILL.md)

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

Resolve a Testability Obstacle

Introduce the smallest behavior-preserving seam needed to test a specific C# behavior, then add deterministic tests that prove both the behavior and the seam. The production edit is a means to the requested test, not an invitation to redesign adjacent code.

When to Use

  • A requested test would otherwise read/write the real filesystem.
  • Behavior depends on the current time, delay, random value, environment, console, process, or another ambient dependency.
  • The user explicitly permits or requests a safe production seam.
  • Existing tests cannot control a dependency without process-global mutation.

When Not to Use

  • The dependency is already injected or passed as an argument. Write tests with a fake through the existing seam using code-testing.
  • The user wants a repository-wide testability audit. Use detect-static-dependencies.
  • The user wants wrappers generated but not call sites/tests changed. Use generate-testability-wrappers.
  • The user requests a broad mechanical migration. Use migrate-static-to-wrapper, then generate tests separately.
  • The user already selected an existing replacement such as TimeProvider or IFileSystem and asks to migrate call sites to it. Use migrate-static-to-wrapper, which also updates affected tests.
  • The code is not C#/.NET.

Inputs

Input Required Description
Behavior to test Yes The method/workflow and expected observable behavior
Target scope No Discover the narrowest relevant file/project when omitted
Allowed production changes No Default to the minimum internal/constructor seam

Workflow

Step 1: Prove the obstacle

Read the target production path and its existing tests. Identify the exact ambient operation preventing a deterministic test and the behavior that must remain unchanged. Do not run a repository-wide static scan for a single-class request.

If an adequate seam already exists, stop refactoring and use it. This skill adds no value when a fake can already be supplied.

Step 2: Select the smallest safe seam

Choose by dependency and repository constraints:

Dependency Preferred seam
Current time / timers Inject TimeProvider; use FakeTimeProvider in tests
Filesystem Existing repository abstraction; for one write/read operation use an injected delegate when conventions allow, otherwise a one-member interface or an already accepted System.IO.Abstractions
HTTP Existing typed HttpClient/handler or IHttpClientFactory seam
Randomness One final generated value: inject Func<int> and keep range selection in the real default; inject Func<int, int, int> only when range arguments are behavior the test must verify; multiple operations/state: inject Random or a minimal generator interface
Environment/console/process Minimal interface containing only members used by the target

The scoped AsyncLocal<T> rule applies to every static API that must retain its public static shape — clocks, filesystem access, environment lookups, identity generation, and randomness. The scope captures and restores the previous value; never implement Dispose() as an unconditional assignment to null. Store the provider/value itself in AsyncLocal<T>. Do not put a mutable Stack<T>, list, or other shared mutable collection in the slot: child execution contexts can inherit the same object and corrupt each other's nesting. When the provider itself is mutable (for example an in-memory store or fake time provider), establish a fresh provider inside each parallel flow rather than mutating one inherited instance from a parent context.

Constructor injection is the default for instance classes. Reuse the repository's DI and naming conventions, but do not add a DI container to a class library just to satisfy this workflow.

Preserve the existing public construction surface unless the user authorizes an API change. Keep a public parameterless constructor as the real-dependency default and place a test-only delegate/provider constructor at the narrowest visibility the test project can reach. Do not turn the seam into a new public optional parameter merely for test convenience.

For a static class or a public API that cannot change, use a scoped ambient seam only when constructor/parameter injection is impossible. The override must:

  • flow across await (AsyncLocal<T>, not `[ThreadS
자주 묻는 질문

코드를 다시 설계하나요?

아니요. 운영 코드 변경은 요청된 테스트에 필요한 것으로 제한합니다.

의존성이 이미 주입되어 있다면요?

그러면 연결점이 필요 없고, 기존 연결점을 통해 가짜로 테스트를 쓰도록 안내합니다.

프로젝트 전체를 마이그레이션하나요?

아니요. 넓은 마이그레이션은 별도의 스킬이 다룹니다.

상세 정보

작성자의 원본 설명

MUST USE for C#/.NET deterministic tests that require the smallest production seam for DateTime/Task.Delay/File/Environment/Guid/Random, static API preservation, nested/parallel overrides, or no real I/O. USE ONLY when the target workspace contains C# source plus a .csproj or .sln. DO NOT USE for audits, bulk migration, code that already has an injectable seam, or an explicit migration to a user-named existing abstraction (migrate-static-to-wrapper). Use instead of general test generation when the requested test is impossible without a production edit and seam selection is still open.

repo: dotnet/skills

인기도

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

이 스킬이 유용한가요?

0

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

댓글

  • …

이 저장소의 다른 스킬

비슷한 스킬