exunit-code-review

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

ExUnit Code Review

ExUnit代码审查

Quick Reference

快速参考

Issue TypeReference
Async tests, setup, describe, tagsreferences/exunit-patterns.md
Behavior-based mocking, expectationsreferences/mox-boundaries.md
Bypass, Swoosh, Oban testingreferences/test-adapters.md
What to mock vs real, Ecto sandboxreferences/integration-tests.md
问题类型参考文档
异步测试、setup、describe、标签references/exunit-patterns.md
基于行为的模拟、预期references/mox-boundaries.md
Bypass、Swoosh、Oban测试references/test-adapters.md
模拟对象与真实对象的选择、Ecto沙箱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
仅在外部边界进行模拟:
  • HTTP客户端、外部API、第三方服务
  • 慢资源:文件系统、邮件、任务队列
  • 非确定性资源:DateTime.utc_now(), :rand
禁止模拟内部代码:
  • 上下文、schemas、GenServers
  • 内部模块、PubSub
  • 所有自研代码

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
  • 除非需要共享数据库状态,否则测试应设置
    async: true
  • 使用describe块对相关测试进行分组
  • 使用setup提取通用测试数据
  • 测试具备清晰的准备/执行/断言(arrange/act/assert)结构

Mocking

模拟实现

  • Mox used for external boundaries (HTTP, APIs)
  • Behaviors defined for mockable interfaces
  • No mocking of internal modules
  • verify_on_exit! in setup for strict mocking
  • 使用Mox对外部边界(HTTP、API)进行模拟
  • 为可模拟接口定义Behaviors
  • 不模拟内部模块
  • 在setup中使用verify_on_exit!实现严格模拟

Test Adapters

测试适配器

  • Bypass for HTTP endpoint mocking
  • Swoosh.TestAdapter for email testing
  • Oban.Testing for background job assertions
  • 使用Bypass模拟HTTP端点
  • 使用Swoosh.TestAdapter进行邮件测试
  • 使用Oban.Testing进行后台任务断言

Database

数据库

  • Ecto.Adapters.SQL.Sandbox for isolation
  • Async tests don't share database state
  • Fixtures/factories used consistently
  • 使用Ecto.Adapters.SQL.Sandbox实现隔离
  • 异步测试不共享数据库状态
  • 一致使用Fixtures/factories

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
  • 单元测试用模拟,集成测试用真实实例 - 不同测试级别有不同需求
  • 集成测试中不模拟数据库 - 数据库属于内部资源
  • 简单的内联测试数据 - 并非所有场景都需要工厂
  • 通过公共API测试私有函数 - 这是正确的做法

Context-Sensitive Rules

上下文敏感规则

IssueFlag ONLY IF
Not asyncTest actually needs shared state
Missing mockExternal call exists AND no mock/bypass
Mock internalModule being mocked is internal code
问题仅在以下情况标记
未使用异步测试确实需要共享状态
缺少模拟存在外部调用且未使用模拟/Bypass
模拟内部代码被模拟的模块是内部代码

Before Submitting Findings

提交发现前的准备

Use the issue format:
[FILE:LINE] ISSUE_TITLE
for each finding.
Load and follow review-verification-protocol before reporting any issue.
每条发现请使用格式:
[文件:行号] 问题标题
。 在报告任何问题前,请加载并遵循审查验证协议