Skip to content

Instantly share code, notes, and snippets.

@ae6rt
Created November 28, 2014 16:28
Show Gist options
  • Save ae6rt/6959dd50e3a98ec0a1c7 to your computer and use it in GitHub Desktop.
Save ae6rt/6959dd50e3a98ec0a1c7 to your computer and use it in GitHub Desktop.
Restore default implementations of code in Go unit tests
If you replace a default function in a package for unit testing purposes, make sure you
restore it so the next test does not mistakenly inherit the reassignment:
func TestEndpointNoScopes(t *testing.T) {
saveAuth := auth
defer func() {
auth = saveAuth
}()
auth = func(*http.Request, []string) (bool, error) {
return false, fmt.Errorf("some error")
}
...
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment