首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >测试接受未在Golang中返回值的回调函数的方法

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

Stack Overflow用户
提问于 2018-08-16 23:44:36
回答 1查看 1.1K关注 0票数 0

我正在尝试测试以下函数:

代码语言:javascript
复制
// 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由以下各项定义:

代码语言:javascript
复制
type OnCompletion func(result CallResultSt, data interface{})

我的大脑立即想到创建一个间谍回调。为此,我对this framework进行了派生,得出了以下结果:

代码语言:javascript
复制
// 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"))

它对我的问候是

代码语言:javascript
复制
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference

我如何纠正这个问题,并测试这个方法呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-17 05:23:59

我会抛弃间谍的东西。这项任务非常简单,您不应该需要外部依赖来处理它。相反,您可以创建自己的"spy“,它在调用函数时将args传递到一个通道中。在您的测试中,然后尝试从通道接收。这将强制测试等待回调函数被调用。您还可以考虑添加超时期限,以便测试可以失败,而不是在从未调用函数的情况下永远阻塞。

代码语言:javascript
复制
// 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

如果你想使用超时:

代码语言:javascript
复制
...
select {
case args := <-spyChan:
    // assertions on args
case <-time.After(5 * time.Second):
    // prevent blocking for over 5 seconds and probably fail the test
}
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51880867

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档