dousi8559 2016-08-24 00:09
浏览 94
已采纳

httptest.ResponseRecorder没有字段或方法Result

So this one is really weird, I'm trying to get a mock response that renders JSON. My test looks like this:

import (
    "fmt"
    "net/http"
    "net/http/httptest"
    "reflect"
    "strings"
    "testing"
)
...

func TestMessageFromResponse(t *testing.T) {
    mc := MyController{}
    body := "{ \"message\":\"kthxbai\" }"
    w := httptest.NewRecorder()
    w.Write([]byte(body))
    resp := w.Result()
    msg := mc.messageFromResponse(resp)
    if msg != "kthxbai" {
        t.Errorf("Expected response body to be kthxbai but was %s", msg)
    }
}

I'm testing the method messageFromResponse on MyControllerbut its not even building. When I run go test in my project directory, I get the following error:

./my_controller_test.go:115: w.Result undefined (type *httptest.ResponseRecorder has no field or method Result)

I should also mention that I am successfully using httptest.ResponseRecorder as a writer stub elsewhere in this same file, but it's just failing when I try to access Result().

  • 写回答

1条回答 默认 最新

  • dsdzz04802 2016-08-24 01:36
    关注

    I initially addressed this as a comment because I was unsure of relevance, but for posterity:

    The online godoc always references the latest release. In this case, the Result() method was added in Go1.7 which only released last week. When in doubt, check your local godoc by running godoc -server or godoc <packagename>.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探