|
| 1 | +package gomock_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "testing" |
| 6 | + "time" |
| 7 | + |
| 8 | + "github.com/golang/mock/gomock" |
| 9 | + mock_sample "github.com/golang/mock/sample/mock_user" |
| 10 | +) |
| 11 | + |
| 12 | +func ExampleCall_DoAndReturn() { |
| 13 | + t := &testing.T{} // provided by test |
| 14 | + ctrl := gomock.NewController(t) |
| 15 | + mockIndex := mock_sample.NewMockIndex(ctrl) |
| 16 | + |
| 17 | + mockIndex.EXPECT().Get(gomock.Any()).DoAndReturn( |
| 18 | + func() string { |
| 19 | + time.Sleep(1 * time.Second) |
| 20 | + return "I'm sleepy" |
| 21 | + }, |
| 22 | + ) |
| 23 | +} |
| 24 | + |
| 25 | +func ExampleCall_DoAndReturn_captureArguments() { |
| 26 | + t := &testing.T{} // provided by test |
| 27 | + ctrl := gomock.NewController(t) |
| 28 | + mockIndex := mock_sample.NewMockIndex(ctrl) |
| 29 | + var s string |
| 30 | + |
| 31 | + mockIndex.EXPECT().Get(gomock.AssignableToTypeOf(s)).DoAndReturn( |
| 32 | + // When capturing arguments the anonymous function should have the same signature as the mocked method. |
| 33 | + func(arg string) interface{} { |
| 34 | + time.Sleep(1 * time.Second) |
| 35 | + fmt.Println(arg) |
| 36 | + return "I'm sleepy" |
| 37 | + }, |
| 38 | + ) |
| 39 | +} |
| 40 | + |
| 41 | +func ExampleCall_Do() { |
| 42 | + t := &testing.T{} // provided by test |
| 43 | + ctrl := gomock.NewController(t) |
| 44 | + mockIndex := mock_sample.NewMockIndex(ctrl) |
| 45 | + |
| 46 | + mockIndex.EXPECT().Anon(gomock.Any()).Do( |
| 47 | + func() { |
| 48 | + time.Sleep(1 * time.Second) |
| 49 | + }, |
| 50 | + ) |
| 51 | +} |
| 52 | + |
| 53 | +func ExampleCall_Do_captureArguments() { |
| 54 | + t := &testing.T{} // provided by test |
| 55 | + ctrl := gomock.NewController(t) |
| 56 | + mockIndex := mock_sample.NewMockIndex(ctrl) |
| 57 | + |
| 58 | + var s string |
| 59 | + mockIndex.EXPECT().Anon(gomock.AssignableToTypeOf(s)).Do( |
| 60 | + // When capturing arguments the anonymous function should have the same signature as the mocked method. |
| 61 | + func(arg string) { |
| 62 | + time.Sleep(1 * time.Second) |
| 63 | + fmt.Println(arg) |
| 64 | + }, |
| 65 | + ) |
| 66 | +} |
0 commit comments