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

Commit 72ff7a4

Browse files
committed
Update error message
1 parent aacf129 commit 72ff7a4

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

gomock/call.go

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -108,17 +108,22 @@ func (c *Call) MaxTimes(n int) *Call {
108108
// DoAndReturn declares the action to run when the call is matched.
109109
// The return values from this function are returned by the mocked function.
110110
// It takes an interface{} argument to support n-arity functions.
111-
// The anonymous function must have the same number of input and output arguments as the mocked method.
111+
// The anonymous function must match the function signature mocked method.
112112
func (c *Call) DoAndReturn(f interface{}) *Call {
113113
// TODO: Check arity and types here, rather than dying badly elsewhere.
114114
v := reflect.ValueOf(f)
115115

116116
c.addAction(func(args []interface{}) []interface{} {
117117
c.t.Helper()
118118
ft := v.Type()
119-
if c.methodType.NumIn() != ft.NumIn() && !ft.IsVariadic() {
120-
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]",
121-
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
119+
if c.methodType.NumIn() != ft.NumIn() {
120+
if ft.IsVariadic() {
121+
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v The function signature must match the mocked method, a variadic function cannot be used.",
122+
c.receiver, c.method)
123+
} else {
124+
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]",
125+
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
126+
}
122127
return nil
123128
}
124129
vArgs := make([]reflect.Value, len(args))
@@ -144,17 +149,22 @@ func (c *Call) DoAndReturn(f interface{}) *Call {
144149
// return values are ignored to retain backward compatibility. To use the
145150
// return values call DoAndReturn.
146151
// It takes an interface{} argument to support n-arity functions.
147-
// The anonymous function must have the same number of input arguments as the mocked method.
152+
// The anonymous function must match the function signature mocked method.
148153
func (c *Call) Do(f interface{}) *Call {
149154
// TODO: Check arity and types here, rather than dying badly elsewhere.
150155
v := reflect.ValueOf(f)
151156

152157
c.addAction(func(args []interface{}) []interface{} {
153158
c.t.Helper()
154159
ft := v.Type()
155-
if c.methodType.NumIn() != ft.NumIn() && !ft.IsVariadic() {
156-
c.t.Fatalf("wrong number of arguments in Do func for %T.%v: got %d, want %d [%s]",
157-
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
160+
if c.methodType.NumIn() != ft.NumIn() {
161+
if ft.IsVariadic() {
162+
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v The function signature must match the mocked method, a variadic function cannot be used.",
163+
c.receiver, c.method)
164+
} else {
165+
c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]",
166+
c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin)
167+
}
158168
return nil
159169
}
160170
vArgs := make([]reflect.Value, len(args))

gomock/call_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -515,7 +515,7 @@ func TestCall_Do_NumArgValidation(t *testing.T) {
515515
methodType: reflect.TypeOf(func(one, two string) {}),
516516
doFn: func(args ...interface{}) {},
517517
args: []interface{}{"just", "right"},
518-
wantErr: false,
518+
wantErr: true,
519519
},
520520
}
521521
for _, tt := range tests {
@@ -571,7 +571,7 @@ func TestCall_DoAndReturn_NumArgValidation(t *testing.T) {
571571
methodType: reflect.TypeOf(func(one, two string) {}),
572572
doFn: func(args ...interface{}) string { return "" },
573573
args: []interface{}{"just", "right"},
574-
wantErr: false,
574+
wantErr: true,
575575
},
576576
}
577577
for _, tt := range tests {

0 commit comments

Comments
 (0)