golang / mock

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

mockgen does not work with type alias #144

Closed yiminc-zz closed 4 years ago

yiminc-zz commented 6 years ago

golang 1.9 introduce this type alias feature where you can define type as alias of another type. There are many different legit use case of that type alias. The mockgen does not seem to be able to generate mocks for type alias.

For example, If i define Foo as interface, then define Bar as alias of Foo, and I want to generate mock for Bar, it won't work.

ChuntaoLu commented 6 years ago

This is not true. Mockgen reflect mode does work with type alias, and it also support type redeclaration.

You can run mockgen /import/path/to/foo A,B to generate mocks for something like

type A Foo
type B = Foo

type Foo interface {
    Get()
}
jmhodges commented 5 years ago

Ah, I think the problem is with source mode. It's non-obvious why that wouldn't work

jmhodges commented 5 years ago

(Well, it is obvious after you remember how source mode works, of course! source mode can't import other packages)

codyoss commented 5 years ago

I think this sounds like a good place for some better docs.

codyoss commented 4 years ago

mockgen does work with alias, but I think only correctly with source mode. This feature request is being tracked in #244. I am going to close this one for now as the other has more active discussion talking place. If there is more to be said, lets say it over there.