Testing 如何在GO中测试响应体?

Testing 如何在GO中测试响应体?,testing,go,Testing,Go,我有以下功能: func SomeFunction(w http.ResponseWriter, ...) error{ . . . return json.NewEncoder(w).Encode(&c) } 其中c是我的结构。 当我运行该函数时,我会收到带有json的响应体,但我无法测试该函数。例如,我有: func TestSomeFunction(t *testing.T) { . . . w := httptest.ResponseRecorde

我有以下功能:

func SomeFunction(w http.ResponseWriter, ...) error{
 .
 .
 .
    return json.NewEncoder(w).Encode(&c)
}
其中c是我的结构。 当我运行该函数时,我会收到带有json的响应体,但我无法测试该函数。例如,我有:

func TestSomeFunction(t *testing.T) {
 .
 .
 .

    w := httptest.ResponseRecorder{}
    err := SomeFunction(w, ...)
 .
 .
}

在这种情况下,我的w.身体是空的。问题不在于我的功能,因为我跑步时得到的是身体。我想我的测试方式不对。测试它的正确方法是什么?

您应该使用
httptest.NewRecorder()
来获取记录器。。。。此外,我不会对记录器的
字节进行测试。。。。检查
结果

您应该使用
httptest.NewRecorder()
获取记录器。。。。此外,我不会对记录器的
字节进行测试。。。。检查
结果