golang / mock

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

Allow mocks from several packages in one mock file #542

Closed cwmos closed 3 years ago

cwmos commented 3 years ago

As far as I can see, if you use gomock in reflect mode, all mocked interfaces that end up in one gomock output file, must come from the same package.

I would like it to be possible to mock interfaces from several packages and let them end up in one gomock output file. For example by:

mockgen database/sql/driver Conn,Driver net/http Client

Why?

Suppose I have a package A that needs mocks of interfaces defined in packages B, C and D.

The way I currently do this, is that I place the mocks for B, C and D in package A in files mock_B_test.go, mock_C_test.go and mock_D_test.go respectively. I do it this way in order to avoid import cycles and to make sure godoc does not create documentation of the mocks.

However, in this example I need to have three different autogenerated mock files in package A. It would be nice if only one file was needed in order not to clutter the source code with more autogenerated files than strictly needed.

codyoss commented 3 years ago

Hey, thanks for the feature request. I will leave this issue open for a while to see if there is interest for this feature by other users of the library. I personally like the multiple files today as each external library dependency each have their own separate dependencies. I also think that using the CLI commands would become harder to read. I typically put my mockgen statements as go generate directives in source code.

codyoss commented 3 years ago

At this time I don't think we will be moving forward with this request. Besides other comments from above there would be no good way to specify flags for each separate mock. I suggest using multiple go:generate directives.