nyarly / spies

MIT License
0 stars 0 forks source link

Spied method panics when not returning anything #1

Open MikeWarren2014 opened 6 years ago

MikeWarren2014 commented 6 years ago

Upon testing a method that takes a callback function, to do some async operation, I use your library to create a spy:

type MySpy struct {
    *spies.Spy
}

func (my *MySpy) Callback(res CallResultSt, data interface{}) {
    my.Called(res, data)
    fmt.Println("Hello world")
    return
}

The method under test has an implementation like this (although this is extremely simplified):

go func(data interface{}) { 
    callback(apiCore.SendRequest([data here]), data)
}(callbackCustomData)

Immediately after calling the method that does that, I say :

spy.MatchMethod("Callback", spies.AnyArgs, "")

When I run the test, I get greeted with

panic: runtime error: invalid memory address or nil pointer dereference [recovered]
    panic: runtime error: invalid memory address or nil pointer dereference

Somehow, it fails to find the callback that I just defined for it!

There's no good documentation on how to remedy this. The callback taken by the method under test isn't supposed to return anything (we're doing async stuff).

MikeWarren2014 commented 6 years ago

UPDATE: I made that spies.Spy the object, not the dereference of a pointer. It now works, as in it doesn't panic, but now it throws error upon testing asynchronous function.