Mock and restore var assignments
go get gopkg.in/nowk/mockvar.v0
Basic use is to redefine var assigned values then restore them to their original values when requiring a simple way to mock out certain code for tests.
Given:
var getProfile = func() (*Profile, error) {
return &Profile{
Name: "Batman",
}, nil
}
func SaveProfile() error {
p, err := getProfile()
if err != nil {
return err
}
return nil
}
To mock a returned error:
restore := mockvar.Mock(&getProfile, func() (*Profile, error) {
return nil, errors.New("Oops!")
})
defer restore() // restore back to original
err := SaveProfile()
assert.Equal(t, "Oops!", err.Error())
MIT