我正在尝试测试以下功能:
// SendRequestAsync sends request asynchronously, accepts callback
// func, which it invokes
//
// Parameters:
// - `context` : some context
// - `token` : some token
// - `apiURL` : the URL to hit
// - `callType` : the type of request to make. This should be one of
// the HTTP verbs (`"GET"`, `"POST"`, `"PUT"`, `"DELETE"`, ...)
// - `callBack` : the func to invoke upon completion
// - `callBackCustomData`: the data to invoke `callBack` with
//
// Since this is an async request, it doesn't return anything.
func (a *APICoreSt) SendRequestAsync(context interface{}, token string, apiURL string, callType APIType, header map[string]string, jsonBody []byte,
callBack OnCompletion, callBackCustomData interface{}) {
go func(data interface{}) {
callBack(a.SendRequest(context, token, apiURL, callType, header, jsonBody), data)
}(callBackCustomData)
}
其中OnCompletion定义为:
type OnCompletion func(result CallResultSt, data interface{})
我立刻想到创建一个间谍回调。为此,我分叉了这个框架,提出了以下内容:
// outside the test function
type MySpy struct {
*spies.Spy
}
func (my *MySpy) Callback(res CallResultSt, data interface{}) {
my.Called(res, data)
fmt.Println("Hello world")
return
}
//in the test function
spy := new(MySpy)
//...some table-driven test logic the generator came up with, containing my data
spy.MatchMethod("Callback", spies.AnyArgs)
assert.NotEmpty(t, spies.CallsTo("Callback"))
它向我打招呼
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
我该如何解决这个问题,并测试这个方法?
喵喔喔
相关分类