【发布时间】:2017-04-05 20:24:40
【问题描述】:
我正在尝试寻找一种解决方案来编写测试和模拟 HTTP 响应。 在我接受接口的函数中:
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
我使用基本身份验证发出 http 获取请求
func GetOverview(client HttpClient, overview *Overview) (*Overview, error) {
request, err := http.NewRequest("GET", fmt.Sprintf("%s:%s/api/overview", overview.Config.Url, overview.Config.Port), nil)
if (err != nil) {
log.Println(err)
}
request.SetBasicAuth(overview.Config.User, overview.Config.Password)
resp, err := client.Do(request)
如何模拟这个 HttpClient? 我正在寻找模拟库,例如:https://github.com/h2non/gock 但只有获取和发布的模拟
也许我应该换一种方式。 我会很感激你的建议
【问题讨论】:
标签: unit-testing testing go mocking