Created
October 27, 2015 10:03
-
-
Save daniellowtw/ec299f4771556f5e3bd1 to your computer and use it in GitHub Desktop.
Mocking in Go
This file contains 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 ( | |
"golang.org/x/net/context" | |
"github.com/stretchr/testify/mock" | |
) | |
type Foo interface { | |
Bar (ctx context.Context) (string) | |
} | |
type MockFoo struct{ | |
mock.Mock | |
Foo | |
} | |
// Feels like it is mainly defining the signature | |
func (m MockFoo) Bar (ctx context.Context) (context.Context) { | |
// args:= m.Called("test") // This says we can do mock.On("Bar", "test") <-- see (1) below | |
args:= m.Called(mock.Anything) // This says we can do mock.On("Bar", _) | |
return args.Get(0).(context.Context) // This says we can do mock.On(..).Return(something of type Context) | |
} | |
func main() { | |
a := MockFoo{} | |
// a.On("Bar", "test").Return(context.TODO()) <-- This would have worked if we enabled the commented line above. | |
a.On("Bar", context.TODO()).Return(context.TODO()) | |
a.Bar(context.TODO()) | |
println("OK") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment