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

时间:2018-08-16 15:44:36

标签: unit-testing go spy

我正在尝试测试以下功能:

// 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{})

我立即想到创建一个间谍回调。为此,我分叉了this framework,并提出了以下建议:

// 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

我该如何补救并测试此方法?

1 个答案:

答案 0 :(得分:1)

我会抛弃间谍的东西。该任务非常简单,您不需要外部依赖项即可处理。相反,您可以使自己的“间谍”具有一个在调用函数时将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
}