我正在尝试使用Mockery来模拟Http客户端,但是当我尝试传递返回值时,我得到了一个错误消息:type http.Response is not an expression
下面是我的接口和处理程序
type HTTPClient interface {
Get(url string) (resp *http.Response, err error)
}
type Handler struct {
httpClient HTTPClient
}Mockery生成了一个mock类,如下所示
type HTTPClient struct {
mock.Mock
}
// Get provides a mock function with given fields: url
func (_m *HTTPClient) Get(url string) (*http.Response, error) {
ret := _m.Called(url)
var r0 *http.Response
if rf, ok := ret.Get(0).(func(string) *http.Response); ok {
r0 = rf(url)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*http.Response)
}
}
var r1 error
if rf, ok := ret.Get(1).(func(string) error); ok {
r1 = rf(url)
} else {
r1 = ret.Error(1)
}
return r0, r1
}在我的测试中,我尝试模拟httpclient,如下所示
httpClient := &mocks.HTTPClient{}
httpClient.On("Get", request.SubscribeURL).Return(resp*http.Response, nil)我不确定我应该返回什么参数。我应该传递什么?
发布于 2020-01-24 21:58:12
http.Response (注意它没有方括号,它主要表示一个类型)是一个表达式,而您应该返回实际的response (函数的返回值),因为一个函数返回两个值*http.Response一个指向http响应结构的指针和一个error或nil。
body := ioutil.NopCloser(strings.NewReader("hello world"))
httpRespose := &http.Response{Body: body, Status: "200 OK", StatusCode: 200}
httpClient.On("Get", request.SubscribeURL)
.Return(
httpRespose, // http response with string in the body and the code 200
nil, // no error occured while response processed
)https://stackoverflow.com/questions/59897105
复制相似问题