Stub
在单元测试中,stub 提供预设返回值,隔离被测代码与外部依赖(数据库、API)。与 mock 相近,但 stub 侧重“假实现”,不一定验证调用次数。网络编程中 stub 也指客户端侧的 RPC 代理代码。
- “We used a stub for the payment service during unit tests.” (单元测试里我们用支付服务的 stub。)
- “The stub always returns a fixed user profile for testing.” (该 stub 在测试中固定返回一份用户资料。)
- “gRPC generates client stubs from the proto file.” (gRPC 根据 proto 文件生成客户端 stub。)
源自古英语 stybb(树桩),比喻“短小的替代物”;测试术语中强调简化替身。
该词无典型词缀;技术义为隐喻延伸。
测试替身家族包括 stub、mock、fake、spy;Martin Fowler 等文献对细微差别有专门讨论。
- 固定搭配: “test stub” (测试桩), “service stub” (服务桩), “stub implementation” (桩实现)
- 相关: mock, fake, double
像树桩 stub 代替完整树干,stub 代替真实依赖的“短实现”。
“The database stub returned canned rows so the report logic could be tested offline.” (数据库 stub 返回预制行,报表逻辑得以离线测试。)