Testing Anti-Patterns by bobmatnyc/claude-mpm-skills
npx skills add https://github.com/bobmatnyc/claude-mpm-skills --skill 'Testing Anti-Patterns'测试必须验证真实行为,而非模拟行为。模拟(Mock)是隔离的手段,而非被测试的对象。
核心原则: 测试代码的实际功能,而非模拟对象的行为。
遵循严格的测试驱动开发(TDD)可以防止这些反模式。 关于完整的 TDD 工作流,请参见技能库中的测试驱动开发技能。
在以下情况下激活此技能:
断言模拟元素而非真实行为。修复方法: 测试真实组件,或者不模拟它。→ core-anti-patterns.md
广告位招租
在这里展示您的产品或服务
触达数万 AI 开发者,精准高效
生产类中仅被测试使用的方法。修复方法: 移至测试工具类。→ core-anti-patterns.md
在不理解依赖关系/副作用的情况下进行模拟。修复方法: 先理解,再最小化模拟。→ core-anti-patterns.md
部分模拟缺少下游代码所需的字段。修复方法: 镜像完整的 API 结构。→ completeness-anti-patterns.md
实现“完成”了却没有测试。修复方法: 采用 TDD - 先写测试。→ completeness-anti-patterns.md
在提交任何测试之前运行此清单:
与语言无关的检查:
□ 我是否在断言模拟行为而非真实行为?
→ TypeScript: testId='*-mock', expect(mock).toHaveBeenCalled()
→ Python: mock.assert_called(), mock.call_count
→ 如果是:立即停止 - 测试真实行为或取消模拟
□ 这个方法是否仅因测试而存在?
→ TypeScript: destroy(), reset(), clear() 仅出现在 *.test.ts 中
→ Python: _set_mock_*, _for_testing 仅出现在 test_*.py 中
→ 如果是:立即停止 - 移至测试工具类
□ 我是否完全理解我正在模拟的对象?
→ 如果不是:立即停止 - 先用真实实现运行,然后最小化模拟
□ 我的模拟是否缺少真实 API 拥有的字段?
→ TypeScript: Partial<T>, 不完整的对象
→ Python: Mock() 属性很少,缺少嵌套字段
→ 如果是:立即停止 - 镜像完整的 API 结构
□ 我是否在写测试之前就写了实现?
→ 如果是:立即停止 - 删除实现,先写测试(TDD)
□ 模拟设置是否占测试代码的 50% 以上?
→ 如果是:考虑使用真实组件进行集成测试
参见: detection-guide.md 以获取全面的危险信号和警告迹象。
模拟是隔离的工具,而非测试的对象。
测试模拟行为表明存在问题。修复方法:测试真实行为,或者质疑为何需要模拟。
TDD 可以防止这些模式。 先写测试 → 观察失败 → 最小化实现 → 通过 → 重构。
使用此技能时,请考虑以下互补技能(如果已部署在您的技能包中):
注意:所有技能均可独立部署。此技能无需它们即可完全运行。
在以下情况下立即停止:
*-mock 测试 ID,expect(mock).toHaveBeenCalled()mock.assert_called(), mock.call_count 而没有真实行为检查destroy(), reset() 仅出现在 *.test.ts 中_set_mock_*, _for_testing 带有“仅用于测试”的文档字符串@patch 或 vi.mock() 仅仅是为了“保险起见”Partial<T>, 缺少嵌套对象Mock(),缺少必需字段当模拟变得过于复杂时: 考虑使用真实组件进行集成测试。通常更简单且更有价值。
先决条件: 测试驱动开发技能 - TDD 防止反模式(推荐用于完整工作流) 互补技能: 完成前验证技能 - 测试 = 完成(确保正确的测试规程) 领域特定: webapp-testing, backend-testing 用于框架模式(如果技能库可用,请参见)
每周安装数
0
代码仓库
GitHub 星标数
18
首次出现
1970年1月1日
安全审计
Tests must verify real behavior, not mock behavior. Mocks are a means to isolate, not the thing being tested.
Core principle: Test what the code does, not what the mocks do.
Following strict TDD prevents these anti-patterns. See the Test-Driven Development skill (available in the skill library) for the complete TDD workflow.
Activate this skill when:
1. NEVER test mock behavior
2. NEVER add test-only methods to production classes
3. NEVER mock without understanding dependencies
4. NEVER create incomplete mocks
5. NEVER treat tests as afterthought
Asserting on mock elements instead of real behavior. Fix: Test real component or don't mock it. → core-anti-patterns.md
Methods in production classes only used by tests. Fix: Move to test utilities. → core-anti-patterns.md
Mocking without understanding dependencies/side effects. Fix: Understand first, mock minimally. → core-anti-patterns.md
Partial mocks missing fields downstream code needs. Fix: Mirror complete API structure. → completeness-anti-patterns.md
Implementation "complete" without tests. Fix: TDD - write test first. → completeness-anti-patterns.md
Run this checklist before committing any test:
Language-agnostic checks:
□ Am I asserting on mock behavior instead of real behavior?
→ TypeScript: testId='*-mock', expect(mock).toHaveBeenCalled()
→ Python: mock.assert_called(), mock.call_count
→ If yes: STOP - Test real behavior or unmock
□ Does this method only exist for tests?
→ TypeScript: destroy(), reset(), clear() only in *.test.ts
→ Python: _set_mock_*, _for_testing only in test_*.py
→ If yes: STOP - Move to test utilities
□ Do I fully understand what I'm mocking?
→ If no: STOP - Run with real impl first, then mock minimally
□ Is my mock missing fields the real API has?
→ TypeScript: Partial<T>, incomplete objects
→ Python: Mock() with few attributes, missing nested fields
→ If yes: STOP - Mirror complete API structure
□ Did I write implementation before test?
→ If yes: STOP - Delete impl, write test first (TDD)
□ Is mock setup >50% of test code?
→ If yes: Consider integration test with real components
See: detection-guide.md for comprehensive red flags and warning signs.
Mocks are tools to isolate, not things to test.
Testing mock behavior indicates a problem. Fix: Test real behavior or question why mocking is necessary.
TDD prevents these patterns. Write test first → Watch fail → Minimal implementation → Pass → Refactor.
When using this skill, consider these complementary skills (if deployed in your skill bundle):
test-driven-development : Complete TDD workflow and red-green-refactor cycle
verification-before-completion : Definition of "done" and verification protocols
Note: All skills are independently deployable. This skill is fully functional without them.
STOP immediately when:
*-mock test IDs, expect(mock).toHaveBeenCalled()mock.assert_called(), mock.call_count without real behavior checksdestroy(), reset() only in *.test.ts_set_mock_*, _for_testing with "For testing only" docstringsWhen mocks become too complex: Consider integration tests with real components. Often simpler and more valuable.
Prerequisite: Test-Driven Development skill - TDD prevents anti-patterns (recommended for complete workflow) Complementary: Verification-Before-Completion skill - Tests = done (ensures proper testing discipline) Domain-specific: webapp-testing, backend-testing for framework patterns (see skill library if available)
Weekly Installs
0
Repository
GitHub Stars
18
First Seen
Jan 1, 1970
Security Audits
agent-browser 浏览器自动化工具 - Vercel Labs 命令行网页操作与测试
140,500 周安装
冷邮件撰写指南:如何写出高回复率的个性化销售邮件 | 营销技能
19,500 周安装
TypeScript 高级类型完全指南:泛型、条件类型与映射类型实战
19,000 周安装
Anthropic品牌样式指南 - 官方品牌标识、颜色、字体排版与视觉设计资源
19,900 周安装
AI SEO优化指南:让内容被ChatGPT、Google AI概览等AI系统引用为来源
21,200 周安装
Firecrawl CLI:AI优化的网络爬虫工具,支持搜索、抓取与页面交互
21,300 周安装
完成开发分支技能:自动化Git工作流,高效合并、推送PR或清理分支
24,900 周安装
@patch or vi.mock() "just to be safe"Partial<T>, missing nested objectsMock() for data objects, missing required fields