【问题标题】:GoLang Mocking CSV Rest ResponseGoLang 模拟 CSV 休息响应
【发布时间】:2021-08-16 02:13:12
【问题描述】:

我正在尝试对返回 CSV 响应的 API 进行单元测试。我习惯于通过执行以下操作来测试返回 Json 响应的 API:

resBody := `{"access_token": "token","instance_url": "url","id": "id","token_type": "Bearer"}`
r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))

clientMock.GetMock = func(url string) (*http.Response, error) {
        return &http.Response{
            StatusCode: 200,
            Body:       r,
        }, nil
    }

这里,resBody 是我模拟的 Json 响应,但是无法弄清楚用于测试返回 csv 响应的 API 的等效响应结构。

【问题讨论】:

    标签: unit-testing go


    【解决方案1】:

    没关系,现在可以解决了。 下面可用于模拟 CSV 响应:

    resBody := `"access_token","instance_url","id","token_type"` + "\n" + `"token","url","id","Bearer"`
    r := ioutil.NopCloser(bytes.NewReader([]byte(resBody)))
    
    clientMock.GetMock = func(url string) (*http.Response, error) {
            return &http.Response{
                StatusCode: 200,
                Body:       r,
            }, nil
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 1970-01-01
      • 2018-05-31
      • 2021-09-22
      • 2020-05-05
      • 1970-01-01
      • 2020-03-17
      相关资源
      最近更新 更多