只需为每个测试启动一个新路由器,然后注册要测试的处理程序,然后将测试请求传递给路由器(而不是处理程序),以便路由器可以解析路径参数并将它们传递给处理程序。
func TestGetBook(t *testing.T) { handler := controllers.NewBookController() router := httprouter.New() router.GET("/book/:id", handler.GetBook) req, _ := http.NewRequest("GET", "/book/sampleid", nil) rr := httptest.NewRecorder() router.ServeHTTP(rr, req) if status := rr.Code; status != http.StatusOK { t.Errorf("Wrong status") }}


