This repository was archived by the owner on Jun 27, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 609
Type-safe return values #427
Labels
Comments
Return operates on a |
@codyoss We can add a stub type in generated code to accomplish this, just like methods of sourcetype Auth interface {
// GetByToken return an authorized user by a valid access token.
GetByToken(ctx context.Context, token string) (model.User, error)
} mocks// Code generated by MockGen. DO NOT EDIT.
type MockAuthMockRecorder struct {
...
}
// GetByToken indicates an expected call of GetByToken.
func (mr *MockAuthMockRecorder) GetByToken(ctx, token interface{}) *TypedCall {
mr.mock.ctrl.T.Helper()
return &TypedCall{mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "GetByToken",
reflect.TypeOf((*MockAuth)(nil).GetByToken), ctx, token)}
}
type TypedCall struct {
*gomock.Call
}
func (t *TypedCall) Return(typedReturn1 model.User, typedReturn2 error) *gomock.Call {
return t.Call.Return(typedReturn1, typedReturn2)
} testfunc TestTypeSafeReturn(t *testing.T) {
m := NewMockAuth(gomock.NewController(t))
m.GetByToken(gomock.Any(), gomock.Any()).Return() <== Now we get compile time error
} |
Sign up for free
to subscribe to this conversation on GitHub.
Already have an account?
Sign in.
Requested feature
I have recently moved from
testify/mockery
to using GoMock for generating mocks, and simply having the mock aware of the types of my calls is great.However, the arguments for
Return()
are alwaysinterface{}
. Can gomock generate mocks thatReturn
with types that match the underlying method?This would make refactoring existing code a bit easier as invalid types returned from mocks would be straight compile errors rather than test failures, thus failing faster and with (imo) a clearer error message.
The text was updated successfully, but these errors were encountered: