Skip to content
This repository was archived by the owner on Jun 27, 2023. It is now read-only.

Commit d2fe5cd

Browse files
authored
Add example for Call.Do and Call.DoAndReturn (#470)
1 parent 7b53c4d commit d2fe5cd

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

README.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,11 +167,15 @@ func TestFoo(t *testing.T) {
167167

168168
m := NewMockFoo(ctrl)
169169

170-
// Does not make any assertions. Returns 101 when Bar is invoked with 99.
170+
// Does not make any assertions. Executes the anonymous functions and returns
171+
// its result when Bar is invoked with 99.
171172
m.
172173
EXPECT().
173174
Bar(gomock.Eq(99)).
174-
Return(101).
175+
DoAndReturn(func(_ int) int {
176+
time.Sleep(1*time.Second)
177+
return 101
178+
}).
175179
AnyTimes()
176180

177181
// Does not make any assertions. Returns 103 when Bar is invoked with 101.

gomock/doc_test.go

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
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_latency() {
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+
// signature of anonymous function must have the same number of input and output arguments as the mocked method.
19+
func(arg string) string {
20+
time.Sleep(1 * time.Millisecond)
21+
return "I'm sleepy"
22+
},
23+
)
24+
25+
r := mockIndex.Get("foo")
26+
fmt.Println(r)
27+
// Output: I'm sleepy
28+
}
29+
30+
func ExampleCall_DoAndReturn_captureArguments() {
31+
t := &testing.T{} // provided by test
32+
ctrl := gomock.NewController(t)
33+
mockIndex := mock_sample.NewMockIndex(ctrl)
34+
var s string
35+
36+
mockIndex.EXPECT().Get(gomock.AssignableToTypeOf(s)).DoAndReturn(
37+
// signature of anonymous function must have the same number of input and output arguments as the mocked method.
38+
func(arg string) interface{} {
39+
s = arg
40+
return "I'm sleepy"
41+
},
42+
)
43+
44+
r := mockIndex.Get("foo")
45+
fmt.Printf("%s %s", r, s)
46+
// Output: I'm sleepy foo
47+
}
48+
49+
func ExampleCall_Do_latency() {
50+
t := &testing.T{} // provided by test
51+
ctrl := gomock.NewController(t)
52+
mockIndex := mock_sample.NewMockIndex(ctrl)
53+
54+
mockIndex.EXPECT().Anon(gomock.Any()).Do(
55+
// signature of anonymous function must have the same number of input and output arguments as the mocked method.
56+
func(_ string) {
57+
fmt.Println("sleeping")
58+
time.Sleep(1 * time.Millisecond)
59+
},
60+
)
61+
62+
mockIndex.Anon("foo")
63+
// Output: sleeping
64+
}
65+
66+
func ExampleCall_Do_captureArguments() {
67+
t := &testing.T{} // provided by test
68+
ctrl := gomock.NewController(t)
69+
mockIndex := mock_sample.NewMockIndex(ctrl)
70+
71+
var s string
72+
mockIndex.EXPECT().Anon(gomock.AssignableToTypeOf(s)).Do(
73+
// signature of anonymous function must have the same number of input and output arguments as the mocked method.
74+
func(arg string) {
75+
s = arg
76+
},
77+
)
78+
79+
mockIndex.Anon("foo")
80+
fmt.Println(s)
81+
// Output: foo
82+
}

0 commit comments

Comments
 (0)