Created
November 3, 2013 00:40
-
-
Save ClayShentrup/7285151 to your computer and use it in GitHub Desktop.
Stubbing in Go (Golang)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"fmt" | |
"reflect" | |
"time" | |
) | |
func main() { | |
stubPrototype := func(in []reflect.Value) []reflect.Value { | |
return []reflect.Value{reflect.ValueOf(time.Now())} | |
} | |
makeStub := func(stubbed interface{}) interface{} { | |
return reflect.MakeFunc(reflect.TypeOf(stubbed), stubPrototype).Interface() | |
} | |
timeStub := makeStub(time.Now).(func() time.Time) | |
fmt.Println(timeStub()) | |
} |
webdev
Of course not. The point is only to make a stub that you can pass to verify that something got called.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This totally works when you run it in the standalone program, but it doesn't seem like stubbed time.Now actually gets called from prod code