Skip to content

Instantly share code, notes, and snippets.

@ClayShentrup
Created November 3, 2013 00:40
Show Gist options
  • Select an option

  • Save ClayShentrup/7285151 to your computer and use it in GitHub Desktop.

Select an option

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())
}
@webdev

webdev commented Nov 4, 2013

Copy link
Copy Markdown

Pretty awesome, but crazy!

@webdev

webdev commented Nov 5, 2013

Copy link
Copy Markdown

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

@ClayShentrup

Copy link
Copy Markdown
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