您正在与语言作斗争,并将您的OOP爱好带入并非为此设计的语言。
我个人会改变方向,并使用老式的良好平面结构和功能。
虽然,如果您想继续进行设计,则可以模拟整个接口而不是接口
http。在测试实际
http有效负载而不是对接口进行调用时,可以更加自信地测试代码。
注入
HttpClient到
Vehicle:
func 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 routesresult.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()



