Skip to content

Commit

Permalink
gostub: adding docs on SetEnv (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffbean authored and prashantv committed Oct 7, 2019
1 parent 4f5574e commit bbe3712
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
14 changes: 11 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ static variable, use the Stub function:
var configFile = "config.json"

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

// Test code
Expand All @@ -42,15 +42,15 @@ the static function, and using that local variable to call the static function:
var timeNow = time.Now

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

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

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

Expand All @@ -75,6 +75,14 @@ var osHostname = osHostname
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:

```go
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.
Expand Down
9 changes: 9 additions & 0 deletions doc.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ 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) {
Expand Down Expand Up @@ -46,6 +47,13 @@ StubFunc can also be used to stub functions that return multiple values:
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.
Expand Down Expand Up @@ -75,4 +83,5 @@ and a value which can be assigned to the variable.
*/
package gostub

// FIXME: godocdown no longer works with go syntax highlighting.
//go:generate godocdown -template README.md.tmpl --output=README.md

0 comments on commit bbe3712

Please sign in to comment.