How can I test doStuff
function? (Playground: http://play.golang.org/p/aPFSlaBLgX)
package myPackage
var locked = false
func doStuff() {
defer unlock()
lock()
// some task that can cause errors
// need to test if lock was really unlocked
// this is just a simple example, things can go complex on real world
panic("!")
}
func lock() {
locked = true
}
func unlock() {
locked = false
}
In other words: how to test code that uses defer
statements? What general strategies should be used to test deferred calls? If there are no general practice, how to test this specific code?
PS: Go playground only allows package main