1 回答
TA贡献1845条经验 获得超8个赞
您正在与语言作斗争,并将您的 OOP 爱好带入并非为此而设计的语言。
我个人会改变方向并使用旧的良好扁平结构和功能。
虽然,如果你想继续你的设计,你可以模拟整个http堆栈而不是界面。与调用接口相比,测试真实有效http负载时,您可以更有信心地测试代码。
注入:HttpClient_Vehiclefunc NewVehicle(httpClient *http.Client){}
在测试代码中,使用*http.ServeMux:
mux.Handle("/path1", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// assessments and mocked response
}))
mux.Handle("/path2", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
// assessments and mocked response
}))
// fallback to show not implemented routes
result.mux.Handle("/", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
result.t.Errorf("Not Supported route %q", r.URL.Path)
}))
构建 Http 服务器:
server := httptest.NewServer(mux)
从多路复用服务器创建 Http 客户端:
client := server.Client()
- 1 回答
- 0 关注
- 95 浏览
添加回答
举报