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 關注
- 118 瀏覽
添加回答
舉報