...
1 package gomock_test
2
3
4
5 import (
6 "fmt"
7 "testing"
8 "time"
9
10 "github.com/golang/mock/gomock"
11 )
12
13 type Foo interface {
14 Bar(string) string
15 }
16
17 func ExampleCall_DoAndReturn_latency() {
18 t := &testing.T{}
19 ctrl := gomock.NewController(t)
20 mockIndex := NewMockFoo(ctrl)
21
22 mockIndex.EXPECT().Bar(gomock.Any()).DoAndReturn(
23 func(arg string) string {
24 time.Sleep(1 * time.Millisecond)
25 return "I'm sleepy"
26 },
27 )
28
29 r := mockIndex.Bar("foo")
30 fmt.Println(r)
31
32 }
33
34 func ExampleCall_DoAndReturn_captureArguments() {
35 t := &testing.T{}
36 ctrl := gomock.NewController(t)
37 mockIndex := NewMockFoo(ctrl)
38 var s string
39
40 mockIndex.EXPECT().Bar(gomock.AssignableToTypeOf(s)).DoAndReturn(
41 func(arg string) interface{} {
42 s = arg
43 return "I'm sleepy"
44 },
45 )
46
47 r := mockIndex.Bar("foo")
48 fmt.Printf("%s %s", r, s)
49
50 }
51
View as plain text