为了账号安全,请及时绑定邮箱和手机立即绑定

在 Golang 中测试接受不返回值的回调函数的方法

在 Golang 中测试接受不返回值的回调函数的方法

Go
胡子哥哥 2023-03-21 15:22:38
我正在尝试测试以下功能:// 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 functiontype MySpy struct {    *spies.Spy}func (my *MySpy) Callback(res CallResultSt, data interface{}) {    my.Called(res, data)    fmt.Println("Hello world")    return}//in the test functionspy := new(MySpy)//...some table-driven test logic the generator came up with, containing my dataspy.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我该如何解决这个问题,并测试这个方法?
查看完整描述

1 回答

?
喵喔喔

TA贡献1735条经验 获得超5个赞

我会放弃间谍的东西。此任务非常简单,您不需要外部依赖项来处理它。您可以改为制作自己的“间谍”,它有一个通道,它在调用函数时将 args 传递到其中。在您的测试中,您然后尝试从频道接收。这将强制测试等待回调函数被调用。您还可以考虑添加一个超时时间,这样测试就可以失败,而不是在函数从未被调用时永远阻塞。


// outside the test function

type MySpy struct {

    Args chan MySpyArgs

}


type MySpyArgs struct {

    Res  CallResultSt

    Data interface{}            

}


func (my *MySpy) Callback(res CallResultSt, data interface{}) {

    my.Args <- MySpyArgs{Res: res, Data: data}

}


//in the test function

spyChan := make(chan MySpyArgs)

spy := &MySpy{spyChan}


//...some table-driven test logic the generator came up with, containing my data


args := <-spyChan

// can now assert arguments were as you expected, etc.

一个粗略的工作示例:https ://play.golang.org/p/zUYpjXdkz-4 。


如果你想使用超时:


...

select {

case args := <-spyChan:

    // assertions on args

case <-time.After(5 * time.Second):

    // prevent blocking for over 5 seconds and probably fail the test

}


查看完整回答
反对 回复 2023-03-21
  • 1 回答
  • 0 关注
  • 118 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信