Skip to content

Instantly share code, notes, and snippets.

@ClayShentrup
Created November 3, 2013 00:40
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save ClayShentrup/7285151 to your computer and use it in GitHub Desktop.
Save ClayShentrup/7285151 to your computer and use it in GitHub Desktop.
Stubbing in Go (Golang)
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())
}
@ClayShentrup
Copy link
Author

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