Agent skill
dotnet-testing
.NET 测试策略与落地:领域单测、应用层单测、ASP.NET Core 集成测试(WebApplicationFactory/TestServer)、鉴权 mock。
Install this agent skill to your Project
npx add-skill https://github.com/majiayu000/claude-skill-registry/tree/main/skills/other/dotnet-testing-anything-well-ai-repo-template-b0f93d38
SKILL.md
.NET 测试
此 skill 用于建立“分层可测试性”与“可回归的质量门槛”。
何时使用此 Skill
- 为领域模型/应用服务补齐单元测试
- 为 ASP.NET Core API 增加集成测试(真实管道 + HttpClient)
- 需要在测试里替换数据库、注入 fake 外部依赖、mock 认证
关键最佳实践(含权威依据)
1) 集成测试首选 WebApplicationFactory
- 使用
WebApplicationFactory<TEntryPoint>启动 SUT,并用CreateClient()发 HTTP 请求。 - Minimal API 场景如果
Program是隐式 internal,可在Program.cs增加:public partial class Program { }以便测试项目引用。
参考(Microsoft Learn):
- Integration tests in ASP.NET Core(WebApplicationFactory、ContentRoot 推断、mock authentication)
- Test ASP.NET Core MVC apps(建议测试 host 尽量贴近生产 host)
2) 测试命名与风格一致
- 参考仓库 DDD 指令:
MethodName_Condition_ExpectedResult()命名模式。 - 不要写 Arrange/Act/Assert 注释(遵循仓库 C# 指令)。
3) Mock 认证/授权
- 通过
ConfigureTestServices注入测试用 AuthenticationHandler,确保 scheme 与应用一致。
常见反模式
- 只测 Controller,不测领域规则(导致业务规则回归风险高)
- 集成测试依赖真实外部服务、不可重复
- 测试用例共享状态导致互相污染
使用示例(提示语模板)
- “为该 Minimal API 添加集成测试:覆盖 200/400/401,并用 WebApplicationFactory 替换为 InMemory 数据库。”
- “为该聚合的不变量写单测,命名遵循 MethodName_Condition_ExpectedResult。”
Recommended Agent Skills
Expand your agent's capabilities with these related and highly-rated skills.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
agent-ops-spec
Manage specification documents in .agent/specs/. Use when user provides requirements, acceptance criteria, or feature descriptions that need to be tracked and validated against implementation.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-testing
Test strategy, execution, and coverage analysis. Use when designing tests, running test suites, or analyzing test results beyond baseline checks.
agent-ops-state
Maintain .agent state files. Use at session start, after meaningful steps, and before concluding: read/update constitution/memory/focus/issues/baseline consistently.
Didn't find tool you were looking for?