如何对嵌入在结构中的http处理程序进行单元测试?

时间:2018-11-28 00:30:03

标签: go

我有以下结构

type Server struct {
        *http.Server
        chain      core.Blockchainer
        coreServer *network.Server
    }

及其相应的handler

func (s *Server) methodHandler(w http.ResponseWriter, req *Request, reqParams Params) {
.....
}

如何对handler进行单元测试?

1 个答案:

答案 0 :(得分:0)

上面的处理程序

func (s *Server) methodHandler(w http.ResponseWriter, req *Request, reqParams Params) {
.....
}

可以按照以下步骤进行测试

handler := http.HandlerFunc(s.methodHandler)

req := httptest.NewRequest(...) 

w := httptest.NewRecorder()

handler(w, req)

resp := w.Result()

assert.Equal(t, expected, resp)