stub is a simple package for stubbing values in tests. It reassigns a variable or a function reference for the duration of a test, and restores the original value when the test completes.
Idiomatic usage looks like this:
func TestFoo(t *testing.T) {
defer stub.Value(&someGlobal, newValue)()
// test code that uses someGlobal
}
See API Reference for more information.
This package was originally developed as part of git-spice. It has been extracted into a separate library for reuse in other projects.
The package is inspired by prashantv/gostub.
The chief difference is that stub
provides a smaller API surface.
This software is made available under the BSD-3 license. See LICENSE for the full license text.