mock icon indicating copy to clipboard operation
mock copied to clipboard

Generating mocks (reflect mode) fails if interface contains methods with generic types

Open daolis opened this issue 2 years ago • 1 comments

Generating mocks (reflect mode) fails with Failed to format generated source code: if the interface contains methods with generic types.

The generated code contains the full package path as the type. The wrong type is returned from reflect.typeOf (see https://github.com/golang/go/issues/55147)

Reproduceable with

package test

import (
	_ "github.com/golang/mock/gomock"
)

type TestType string

type TestGenericType[T any] struct {
	blaa T
}

//go:generate mockgen -destination zz_generated_mock.go -package mockst . TestGenericInterface

type TestGenericInterface interface {
	TestMethod() TestGenericType[TestType]
}

generated file contains e.g. this method

// TestMethod mocks base method.
func (m *MockTestGenericInterface) TestMethod() test.TestGenericType[github.com/daolis/mockgentest/test.TestType] {
        m.ctrl.T.Helper()
        ret := m.ctrl.Call(m, "TestMethod")
        ret0, _ := ret[0].(test.TestGenericType[github.com/daolis/mockgentest/test.TestType])
        return ret0
}

daolis avatar Sep 19 '22 14:09 daolis

#669 fix it

tra4less avatar Sep 22 '22 13:09 tra4less

Closing issue: see #669

daolis avatar Mar 14 '23 13:03 daolis