You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
You will get the error: cannot use &mockedFoo (value of type *MockIFoo) as foo.IFoo value in argument to doBar: *MockIFoo does not implement foo.IFoo (unexported method baz)compilerInvalidIfaceAssign
If you were to mock into the foo package, everything is fine, but this isn't always possible.
Why the feature is needed
This is needed if you are trying to mock a 3rd party library, and cannot modify their source code to add a mock. This happens to me all of the time when I'm writing tests and need to mock a 3rd party dependency.
(Optional) Proposed solution
The fix is quite simple. The created struct just needs to imbed the mocked interface. In my example:
// MockIFoo is a mock of IFoo interface.typeMockIFoostruct {
foo.IFooctrl*gomock.Controllerrecorder*MockIFooMockRecorder
}
I don't believe doing this will break any existing code.
We may also need to add a flag for the dependency location foo. For example, if foo wasn't local, but was at github.com/adam-hanna/foo we would need to add that to the imports.
The text was updated successfully, but these errors were encountered:
Requested feature
Say you have the following project structure:
And inside
/foo/interface.go
you have:And you try to mock it into your main (or any package other than
foo
):$ mockgen -source=foo/interface.go -destination=mock.go -package=main
And then you try to use it:
You will get the error:
cannot use &mockedFoo (value of type *MockIFoo) as foo.IFoo value in argument to doBar: *MockIFoo does not implement foo.IFoo (unexported method baz)compilerInvalidIfaceAssign
Demo, here.
If you were to mock into the
foo
package, everything is fine, but this isn't always possible.Why the feature is needed
This is needed if you are trying to mock a 3rd party library, and cannot modify their source code to add a mock. This happens to me all of the time when I'm writing tests and need to mock a 3rd party dependency.
(Optional) Proposed solution
The fix is quite simple. The created struct just needs to imbed the mocked interface. In my example:
I don't believe doing this will break any existing code.
We may also need to add a flag for the dependency location
foo
. For example, iffoo
wasn't local, but was at github.com/adam-hanna/foo we would need to add that to the imports.The text was updated successfully, but these errors were encountered: