stubby

package module
v0.0.0-...-9a6ba70 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 27, 2018 License: MIT Imports: 3 Imported by: 0

README

stubby - stubbing for Go (golang)

Go Report GolangCI Travis codecov License Maintenance GoDoc

go get github.com/apibillme/stubby
var timeNow = time.Now

func GetDate() int {
	return timeNow().Day()
}

stubs := stubby.Stub(&timeNow, func() time.Time {
  return time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC)
})
defer stubs.Reset()
// production code:
var osHostname = osHostname

// Test code:
stubs := stubby.StubFunc(&osHostname, "fakehost", nil)
defer stubs.Reset()

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FuncReturning

func FuncReturning(funcType reflect.Type, results ...interface{}) reflect.Value

FuncReturning creates a new function with type funcType that returns results.

Types

type Stubs

type Stubs struct {
	// contains filtered or unexported fields
}

Stubs represents a set of stubbed variables that can be reset.

func New

func New() *Stubs

New returns Stubs that can be used to stub out variables.

func Stub

func Stub(varToStub interface{}, stubVal interface{}) *Stubs

Stub replaces the value stored at varToStub with stubVal. varToStub must be a pointer to the variable. stubVal should have a type that is assignable to the variable.

func StubFunc

func StubFunc(funcVarToStub interface{}, stubVal ...interface{}) *Stubs

StubFunc replaces a function variable with a function that returns stubVal. funcVarToStub must be a pointer to a function variable. If the function returns multiple values, then multiple values should be passed to stubFunc. The values must match be assignable to the return values' types.

func (*Stubs) Reset

func (s *Stubs) Reset()

Reset resets all stubbed variables back to their original values.

func (*Stubs) ResetSingle

func (s *Stubs) ResetSingle(varToStub interface{})

ResetSingle resets a single stubbed variable back to its original value.

func (*Stubs) SetEnv

func (s *Stubs) SetEnv(k, v string) *Stubs

SetEnv the specified environent variable to the specified value.

func (*Stubs) Stub

func (s *Stubs) Stub(varToStub interface{}, stubVal interface{}) *Stubs

Stub replaces the value stored at varToStub with stubVal. varToStub must be a pointer to the variable. stubVal should have a type that is assignable to the variable.

func (*Stubs) StubFunc

func (s *Stubs) StubFunc(funcVarToStub interface{}, stubVal ...interface{}) *Stubs

StubFunc replaces a function variable with a function that returns stubVal. funcVarToStub must be a pointer to a function variable. If the function returns multiple values, then multiple values should be passed to stubFunc. The values must match be assignable to the return values' types.

func (*Stubs) UnsetEnv

func (s *Stubs) UnsetEnv(k string) *Stubs

UnsetEnv unsets the specified environent variable.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL