ExUnit Code Review
Quick Reference
references/mox-boundaries.md |
| Bypass, Swoosh, Oban testing |
references/test-adapters.md |
| What to mock vs real, Ecto sandbox |
references/integration-tests.md |
Mock Boundary Philosophy
Mock at external boundaries:
- - HTTP clients, external APIs, third-party services
- Slow resources: file system, email, job queues
- Non-deterministic: DateTime.utc_now(), :rand
DO NOT mock internal code:
- - Contexts, schemas, GenServers
- Internal modules, PubSub
- Anything you wrote
Review Checklist
Test Structure
- - [ ] Tests are
async: true unless sharing database state - [ ] Describe-blocks group related tests
- [ ] Setup extracts common test data
- [ ] Tests have clear arrange/act/assert structure
Mocking
- - [ ] Mox used for external boundaries (HTTP, APIs)
- [ ] Behaviors defined for mockable interfaces
- [ ] No mocking of internal modules
- [ ] verifyonexit! in setup for strict mocking
Test Adapters
- - [ ] Bypass for HTTP endpoint mocking
- [ ] Swoosh.TestAdapter for email testing
- [ ] Oban.Testing for background job assertions
Database
- - [ ] Ecto.Adapters.SQL.Sandbox for isolation
- [ ] Async tests don't share database state
- [ ] Fixtures/factories used consistently
Valid Patterns (Do NOT Flag)
- - Mock in unit test, real in integration - Different test levels have different needs
- Not mocking database in integration tests - Database is internal
- Simple inline test data - Not everything needs factories
- Testing private functions via public API - Correct approach
Context-Sensitive Rules
| Issue | Flag ONLY IF |
|---|
| Not async | Test actually needs shared state |
| Missing mock |
External call exists AND no mock/bypass |
| Mock internal | Module being mocked is internal code |
Before Submitting Findings
Use the issue format: [FILE:LINE] ISSUE_TITLE for each finding.
Load and follow review-verification-protocol before reporting any issue.
ExUnit 代码审查
快速参考
references/mox-boundaries.md |
| Bypass、Swoosh、Oban 测试 |
references/test-adapters.md |
| 模拟与真实的选择、Ecto 沙箱 |
references/integration-tests.md |
模拟边界理念
在外部边界进行模拟:
- - HTTP 客户端、外部 API、第三方服务
- 慢速资源:文件系统、电子邮件、任务队列
- 非确定性:DateTime.utc_now()、:rand
不要模拟内部代码:
- - 上下文、模式、GenServers
- 内部模块、PubSub
- 任何你编写的代码
审查清单
测试结构
- - [ ] 测试设置为 async: true,除非共享数据库状态
- [ ] Describe 块对相关测试进行分组
- [ ] Setup 提取通用测试数据
- [ ] 测试具有清晰的安排/执行/断言结构
模拟
- - [ ] 使用 Mox 模拟外部边界(HTTP、API)
- [ ] 为可模拟接口定义行为
- [ ] 不模拟内部模块
- [ ] 在 setup 中使用 verifyonexit! 实现严格模拟
测试适配器
- - [ ] 使用 Bypass 模拟 HTTP 端点
- [ ] 使用 Swoosh.TestAdapter 进行电子邮件测试
- [ ] 使用 Oban.Testing 进行后台任务断言
数据库
- - [ ] 使用 Ecto.Adapters.SQL.Sandbox 实现隔离
- [ ] 异步测试不共享数据库状态
- [ ] 一致使用 Fixtures/工厂方法
有效模式(不要标记)
- - 单元测试中模拟,集成测试中使用真实 - 不同测试级别有不同需求
- 集成测试中不模拟数据库 - 数据库是内部组件
- 简单的内联测试数据 - 并非所有内容都需要工厂方法
- 通过公共 API 测试私有函数 - 正确的方法
上下文相关规则
| 问题 | 仅当以下情况时标记 |
|---|
| 非异步 | 测试确实需要共享状态 |
| 缺少模拟 |
存在外部调用且没有模拟/Bypass |
| 模拟内部 | 被模拟的模块是内部代码 |
提交发现之前
使用以下格式提交每个发现:[FILE:LINE] ISSUE_TITLE。
在报告任何问题之前,加载并遵循 review-verification-protocol。