-
Notifications
You must be signed in to change notification settings - Fork 121
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support generating mock for interfaces with generics #128
Comments
Hi @pavleprica . Example from a real project:Interface:type Storage[V any] interface {
Set(ctx context.Context, item V, rowVersion internal.RowVersion) error
Delete(ctx context.Context, item V, rowVersion internal.RowVersion) error
} Mock:...
// MockStorage is a mock of Storage interface.
type MockStorage[V any] struct {
ctrl *gomock.Controller
recorder *MockStorageMockRecorder[V]
}
...
// Set mocks base method.
func (m *MockStorage[V]) Set(ctx context.Context, item V, rowVersion internal.RowVersion) error {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Set", ctx, item, rowVersion)
ret0, _ := ret[0].(error)
return ret0
}
... |
Hey @tulzke, But this doesn't seem like handling of generics by But this is a "custom" implementation of the interface to mock it "manually". Not like you can rely on the Or the example you provided is from the generated code? |
I'm coming across a similar problem I think, I'm trying to create a mock for this interface (a connectrpc handler interface generated from protos): type ServiceHandler interface {
Set(context.Context, *connect.Request[package.SetRequest]) (*connect.Response[package.SetResponse], error)
}
The generated code included with the error:
As you can see from the generated code it doesn't look like |
I executed
|
Seeing exactly the same issue. In my case, the generated code has full package/module paths in the generics qualifiers: // Code generated by MockGen. DO NOT EDIT.
// Source: golang.linkedin.com/config-publish-api/config-publish-api-go/generated/com/linkedin/config/compiledConfigDescriptors (interfaces: Client)
...
func (m *MockClient) Create(arg0 *config.CompiledConfigDescriptor) (*common.CreatedEntity[*golang.linkedin.com/config-publish-api/config-publish-api-go/generated/com/linkedin/config/compiledConfigDescriptors.CompiledConfigDescriptors_ComplexKey], error) {
...
func (m *MockClient) CreateWithContext(arg0 context.Context, arg1 *config.CompiledConfigDescriptor) (*common.CreatedEntity[*golang.linkedin.com/config-publish-api/config-publish-api-go/generated/com/linkedin/config/compiledConfigDescriptors.CompiledConfigDescriptors_ComplexKey], error) { which looks like obvious syntax errors. |
Seeing the same thing |
Same thing, syntax error on generating mocks for interfaces with embedded generics. |
My issues were the same as described by @krak3n and @ahmetb. I'm using connectrpc and saw the full module names in the generated code, creating the syntax errors. But @tra4less's solution of using mockgen \
-source=./pkg/proto/life/v1/lifev1connect/life.connect.go \
-destination=./pkg/subsystem/mocks/mock_subsystem_service.go \
-package=mocks github.com/jonnylangefeld/life/pkg/proto/life/v1/lifev1connect \
SubsystemServiceClient The generated code now had an import like this: lifev1 "github.com/jonnylangefeld/life/pkg/proto/life/v1" And the endpoints correctly used the named import in the generics code: // SayHello mocks base method.
func (m *MockHelloServiceClient) SayHello(arg0 context.Context, arg1 *connect.Request[lifev1.SayHelloRequest]) (*connect.Response[lifev1.SayHelloResponse], error) {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "SayHello", arg0, arg1)
ret0, _ := ret[0].(*connect.Response[lifev1.SayHelloResponse])
ret1, _ := ret[1].(error)
return ret0, ret1
} |
Also ran into this issue in which generated code is:
when it should be:
|
Hey team,
Hope you are doing well.
Stumbled upon on this issue. In code as well as on the archived repo.
To not copy paste too much, is this perhaps on the roadmap for you?
It seems that the previous team was on it, but didn't got to the latest release including it.
The text was updated successfully, but these errors were encountered: