However, in unit tests these sorts of functions are notoriously hard to deal with, since they have side effects. In golang, we can use a defer to capture and assert a panic, like so:
defer func() {
// recover from panic if one occured. Set err to nil otherwise.
err = recover()
}()
There might be better ways to test panics in golang. This is just the hack I'm using for now :)
Oh by the way... a simple optimization? Don't do two defers. You can do it all in one, because recover() returns nil if there is no error. BUT, if I told you that in the beginning, you wouldn't know how defer() really worked now would you !

No comments:
Post a Comment