1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
|
package gomock_test
//go:generate mockgen -destination mock_test.go -package gomock_test -source example_test.go
import (
"fmt"
"testing"
"time"
"go.uber.org/mock/gomock"
)
type Foo interface {
Bar(string) string
String() string
}
func ExampleCall_DoAndReturn_latency() {
t := &testing.T{} // provided by test
ctrl := gomock.NewController(t)
mockIndex := NewMockFoo(ctrl)
mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn(
func(arg string) string {
time.Sleep(1 * time.Millisecond)
return "I'm sleepy"
},
)
r := mockIndex.Bar("foo")
fmt.Println(r)
// Output: I'm sleepy
}
func ExampleCall_DoAndReturn_captureArguments() {
t := &testing.T{} // provided by test
ctrl := gomock.NewController(t)
mockIndex := NewMockFoo(ctrl)
var s string
mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn(
func(arg string) any {
s = arg
return "I'm sleepy"
},
)
r := mockIndex.Bar("foo")
fmt.Printf("%s %s", r, s)
// Output: I'm sleepy foo
}
func ExampleCall_DoAndReturn_withOverridableExpectations() {
t := &testing.T{} // provided by test
ctrl := gomock.NewController(t, gomock.WithOverridableExpectations())
mockIndex := NewMockFoo(ctrl)
var s string
mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn(
func(arg string) any {
s = arg
return "I'm sleepy"
},
)
r := mockIndex.Bar("foo")
fmt.Printf("%s %s", r, s)
// Output: I'm sleepy foo
}
|