Gin http request test 筆記

gin 思路 參考相關文件、 repo ,在設計上的層級類似下列這樣 server => service (interface) => repository (interface) 將 interface 開出來,實作完之後一路使用注入的逐層掛進去。 開出 interface 除了規範 method 之外,還能在後續針對測試時, 方便撰寫 mock 注入。 server | - service | - repository 略 測試 以使用 gin 為例 get const wantGetMsg = "mock hello eric" func TestHelloByName(t *testing.T) { gin.SetMode(gin.TestMode) r := gin.Default() // di mock service service := MockHelloService{} server := NewHelloServer(service) // mount router r.GET("/hello/:name", server.SayHello) rq := httptest.NewRequest(http.MethodGet, "/hello/eric", nil) rw := httptest....

March 1, 2022 · 2 min · Me