que*_*ino 26 testing unit-testing mocking go
我正在尝试找到编写测试和模拟HTTP响应的解决方案.在我接受界面的函数中:
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
Run Code Online (Sandbox Code Playgroud)
我使用base auth进行http get请求
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)
Run Code Online (Sandbox Code Playgroud)
我怎么能模仿这个HttpClient?我正在寻找模拟库,例如:https://github.com/h2non/gock 但是只有模拟Get和Post
也许我应该以不同的方式做到这一点.我会很感激你的建议
Gav*_*vin 28
任何具有与您的界面中的签名匹配的方法的结构都将实现该接口.例如,您可以创建一个结构ClientMock
type ClientMock struct {
}
Run Code Online (Sandbox Code Playgroud)
用这个方法
func (c *ClientMock) Do(req *http.Request) (*http.Response, error) {
return &http.Response{}, nil
}
Run Code Online (Sandbox Code Playgroud)
然后,您可以将此ClientMock结构注入到您的GetOverviewfunc中.这是Go Playground的一个例子.
roc*_*cer 17
该net/http/httptest包是你最好的朋友:
// generate a test server so we can capture and inspect the request
testServer := httptest.NewServer(http.HandlerFunc(func(res http.ResponseWriter, req *http.Request) {
res.WriteHeader(scenario.expectedRespStatus)
res.Write([]byte("body"))
}))
defer func() { testServer.Close() }()
req, err := http.NewRequest(http.MethodGet, testServer.URL, nil)
assert.NoError(t, err)
res, err := http.DefaultClient.Do(req)
assert.NoError(t, err)
assert.Equal(t, scenario.expectedRespStatus, res.StatusCode, "status code should match the expected response")
Run Code Online (Sandbox Code Playgroud)
您必须使用与接口匹配的方法创建结构。模拟通常用于测试目的,因此人们希望能够准备模拟方法的返回值。为了实现这一点,我们使用与方法对应的func 属性创建结构体。
由于您的界面是:
type HttpClient interface {
Do(req *http.Request) (*http.Response, error)
}
Run Code Online (Sandbox Code Playgroud)
等效模拟:
type MockClient struct {
DoFunc func(req *http.Request) (*http.Response, error)
}
func (m *MockClient) Do(req *http.Request) (*http.Response, error) {
if m.DoFunc != nil {
return m.DoFunc(req)
}
return &http.Response{}, nil
}
Run Code Online (Sandbox Code Playgroud)
然后,下一步是编写一些测试。示例在这里。