diff --git a/gomock/call.go b/gomock/call.go index 723d0d5f..04db8f8e 100644 --- a/gomock/call.go +++ b/gomock/call.go @@ -108,7 +108,7 @@ func (c *Call) MaxTimes(n int) *Call { // DoAndReturn declares the action to run when the call is matched. // The return values from this function are returned by the mocked function. // It takes an interface{} argument to support n-arity functions. -// The anonymous function must have the same number of input and output arguments as the mocked method. +// The anonymous function must match the function signature mocked method. func (c *Call) DoAndReturn(f interface{}) *Call { // TODO: Check arity and types here, rather than dying badly elsewhere. v := reflect.ValueOf(f) @@ -116,9 +116,14 @@ func (c *Call) DoAndReturn(f interface{}) *Call { c.addAction(func(args []interface{}) []interface{} { c.t.Helper() ft := v.Type() - if c.methodType.NumIn() != ft.NumIn() && !ft.IsVariadic() { - c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]", - c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin) + if c.methodType.NumIn() != ft.NumIn() { + if ft.IsVariadic() { + 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.", + c.receiver, c.method) + } else { + c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]", + c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin) + } return nil } vArgs := make([]reflect.Value, len(args)) @@ -144,7 +149,7 @@ func (c *Call) DoAndReturn(f interface{}) *Call { // return values are ignored to retain backward compatibility. To use the // return values call DoAndReturn. // It takes an interface{} argument to support n-arity functions. -// The anonymous function must have the same number of input arguments as the mocked method. +// The anonymous function must match the function signature mocked method. func (c *Call) Do(f interface{}) *Call { // TODO: Check arity and types here, rather than dying badly elsewhere. v := reflect.ValueOf(f) @@ -152,9 +157,14 @@ func (c *Call) Do(f interface{}) *Call { c.addAction(func(args []interface{}) []interface{} { c.t.Helper() ft := v.Type() - if c.methodType.NumIn() != ft.NumIn() && !ft.IsVariadic() { - c.t.Fatalf("wrong number of arguments in Do func for %T.%v: got %d, want %d [%s]", - c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin) + if c.methodType.NumIn() != ft.NumIn() { + if ft.IsVariadic() { + 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.", + c.receiver, c.method) + } else { + c.t.Fatalf("wrong number of arguments in DoAndReturn func for %T.%v: got %d, want %d [%s]", + c.receiver, c.method, ft.NumIn(), c.methodType.NumIn(), c.origin) + } return nil } vArgs := make([]reflect.Value, len(args)) diff --git a/gomock/call_test.go b/gomock/call_test.go index e19d1423..c7eb39db 100644 --- a/gomock/call_test.go +++ b/gomock/call_test.go @@ -515,7 +515,7 @@ func TestCall_Do_NumArgValidation(t *testing.T) { methodType: reflect.TypeOf(func(one, two string) {}), doFn: func(args ...interface{}) {}, args: []interface{}{"just", "right"}, - wantErr: false, + wantErr: true, }, } for _, tt := range tests { @@ -571,7 +571,7 @@ func TestCall_DoAndReturn_NumArgValidation(t *testing.T) { methodType: reflect.TypeOf(func(one, two string) {}), doFn: func(args ...interface{}) string { return "" }, args: []interface{}{"just", "right"}, - wantErr: false, + wantErr: true, }, } for _, tt := range tests {