知识卡片
存根与监听拆开读写依赖
GET测试让存根按名字返回分数,POST测试让监听器记录RecordWin调用。这样可先确定处理器需要的存储契约,而不等待真实数据库实现。前者控制输入,后者观察交互;它们通过不证明真实存储正确,最后还需组装测试。
结构图:
类图
classDiagram
class PlayerServer {
-store PlayerStore
+ServeHTTP(w http.ResponseWriter, r *http.Request)
}
class PlayerStore {<<interface>>
+GetPlayerScore(name string) int
+RecordWin(name string)
}
class StubPlayerStore {
-scores map[string]int
-winCalls []string
+GetPlayerScore(name string) int
+RecordWin(name string)
}
class InMemoryPlayerStore {
-store map[string]int
+GetPlayerScore(name string) int
+RecordWin(name string)
}
PlayerServer --> PlayerStore : 依赖注入
PlayerStore <|.. StubPlayerStore : 隐式实现
PlayerStore <|.. InMemoryPlayerStore : 隐式实现
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
在整书笔记中的位置
- · HTTP 服务器
原书出处 · 1 处
- HTTP 服务器 · 查看原书依据
ore struct { scores map[string]int winCalls []string } func (s *StubPlayerStore) GetPlayerScore(name string) int { score := s.scores[name] return scor
参考来源
- 位置:《通过测试学习 Go 编程》HTTP 服务器,spine023(book-sources;extracted/通过测试学习 Go 编程/_epub-src/OEBPS/Text/22.html)。
- 结论依据:原文StubPlayerStore含scores和winCalls,分离检索与记录。
- 原始内容:ore struct { scores map[string]int winCalls []string } func (s *StubPlayerStore) GetPlayerScore(name string) int { score := s.scores[name] return scor