我正在编写一个客户端,用于连接具有REST端点的服务器。客户端需要进行11个不同请求的链式操作以完成一个动作(这是rococo备份系统)。
我使用Go编写客户端,并希望在Go中编写模拟/测试。我不清楚一个名为
我的客户端二进制文件将通过以下方式从shell中运行:
我正在查看jarcoal/httpmock中的高级示例(但我也可以使用其他库)。在示例的结尾处,它说“// do stuff that adds and checks articles”,那是我应该调用
为了将来的参考,我已经粘贴了下面的高级示例。
我使用Go编写客户端,并希望在Go中编写模拟/测试。我不清楚一个名为
func TestMain
的测试应该如何调用客户端的func main()
,以测试完成11个请求的链式操作。我的客户端二进制文件将通过以下方式从shell中运行:
$ client_id=12345 region=apac3 backup
我该如何在测试中设置环境变量并调用func main()
?或者还有其他方法吗? (我善于编写测试,所以这不是问题)我正在查看jarcoal/httpmock中的高级示例(但我也可以使用其他库)。在示例的结尾处,它说“// do stuff that adds and checks articles”,那是我应该调用
main()
的地方吗?为了将来的参考,我已经粘贴了下面的高级示例。
func TestFetchArticles(t *testing.T) {
httpmock.Activate()
defer httpmock.DeactivateAndReset()
// our database of articles
articles := make([]map[string]interface{}, 0)
// mock to list out the articles
httpmock.RegisterResponder("GET", "https://api.mybiz.com/articles.json",
func(req *http.Request) (*http.Response, error) {
resp, err := httpmock.NewJsonResponse(200, articles)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// mock to add a new article
httpmock.RegisterResponder("POST", "https://api.mybiz.com/articles.json",
func(req *http.Request) (*http.Response, error) {
article := make(map[string]interface{})
if err := json.NewDecoder(req.Body).Decode(&article); err != nil {
return httpmock.NewStringResponse(400, ""), nil
}
articles = append(articles, article)
resp, err := httpmock.NewJsonResponse(200, article)
if err != nil {
return httpmock.NewStringResponse(500, ""), nil
}
return resp, nil
},
)
// do stuff that adds and checks articles
}