GithubHelp home page GithubHelp logo

gostub's Introduction

gostub

Build Status GoDoc Coverage Status

gostub is a library to make stubbing in unit tests easy.

Getting started

Import the following package: github.com/prashantv/gostub

Click here to read the API documentation.

Package overview

Package gostub is used for stubbing variables in tests, and resetting the original value once the test has been run.

This can be used to stub static variables as well as static functions. To stub a static variable, use the Stub function:

var configFile = "config.json"

func GetConfig() ([]byte, error) {
    return ioutil.ReadFile(configFile)
}

// Test code
stubs := gostub.Stub(&configFile, "/tmp/test.config")

data, err := GetConfig()
// data will now return contents of the /tmp/test.config file

gostub can also stub static functions in a test by using a variable to reference the static function, and using that local variable to call the static function:

var timeNow = time.Now

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

You can test this by using gostub to stub the timeNow variable:

stubs := gostub.Stub(&timeNow, func() time.Time {
    return time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC)
})
defer stubs.Reset()

// Test can check that GetDate returns 6

If you are stubbing a function to return a constant value like in the above test, you can use StubFunc instead:

stubs := gostub.StubFunc(&timeNow, time.Date(2015, 6, 1, 0, 0, 0, 0, time.UTC))
defer stubs.Reset()

StubFunc can also be used to stub functions that return multiple values:

var osHostname = osHostname
// [...] production code using osHostname to call it.

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

StubEnv can be used to setup environment variables for tests, and the environment values are reset to their original values upon Reset:

stubs := gostub.New()
stubs.SetEnv("GOSTUB_VAR", "test_value")
defer stubs.Reset()

The Reset method should be deferred to run at the end of the test to reset all stubbed variables back to their original values.

You can set up multiple stubs by calling Stub again:

stubs := gostub.Stub(&v1, 1)
stubs.Stub(&v2, 2)
defer stubs.Reset()

For simple cases where you are only setting up simple stubs, you can condense the setup and cleanup into a single line:

defer gostub.Stub(&v1, 1).Stub(&v2, 2).Reset()

This sets up the stubs and then defers the Reset call.

You should keep the return argument from the Stub call if you need to change stubs or add more stubs during test execution:

stubs := gostub.Stub(&v1, 1)
defer stubs.Reset()

// Do some testing
stubs.Stub(&v1, 5)

// More testing
stubs.Stub(&b2, 6)

The Stub call must be passed a pointer to the variable that should be stubbed, and a value which can be assigned to the variable.

gostub's People

Contributors

billf avatar jeffbean avatar nikolasa avatar prashantv avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.