mock icon indicating copy to clipboard operation
mock copied to clipboard

Unexported mock types

Open affanshahid opened this issue 3 years ago • 3 comments

Is it possible to generate unexported mock types? i.e input:

package user

type userRepo interface {}

output:

package user

type mockUserRepo struct {}

This way mocks are not exported by the package especially useful when mocking unexported interfaces

affanshahid avatar Jan 19 '22 16:01 affanshahid

I think a better practice here is actually to either put your mocks in their own package. Or, what I personally do is generate them in the test package. This way dependencies on gomock don't leak into production builds. Hope that helps.

codyoss avatar Jan 19 '22 16:01 codyoss

@codyoss CMIIW but if interface is unexported mocks won't be able to live in a separate or test package.

mslusarczyk avatar Mar 01 '22 08:03 mslusarczyk

Guys, i found an idea how to solve this problem with golang generics!

For example:

type boo struct{}

type fooer interface {
  foo() boo
}

And after go:generate

type Mockfooer[T any] struct {
ctrl     *gomock.Controller
	recorder *MockfooerMockRecorder[T]
}

Well, after that it is possible to put your mock to another package and use it like

mock.NewfooerMock[boo](...)

AndrewShukhtin avatar Jun 07 '23 09:06 AndrewShukhtin