golang / mock

GoMock is a mocking framework for the Go programming language.
Apache License 2.0
9.26k stars 608 forks source link

Unexported mock types #615

Open affanshahid opened 2 years ago

affanshahid commented 2 years ago

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

codyoss commented 2 years ago

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.

mslusarczyk commented 2 years ago

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

AndrewShukhtin commented 1 year ago

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](...)