golang / mock

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

How to mock a struct in a library with mockgen? #332

Closed evgeniyasti closed 4 years ago

evgeniyasti commented 4 years ago

Hello.

I want to mock this class: https://github.com/segmentio/kafka-go/blob/master/dialer.go

I'm spinning in circles without any tangible results. Can someone please give me a hint on how to mock the Dialer struct and its functions?

Thanks

codyoss commented 4 years ago

Hey, I think you want should try to create an Interface that Dialer satisfies. Then you should be able to mock it!

ensonic commented 4 years ago

Sorry, but I think this deserves an example. If we continue with the above example,

any function that takes a kafka.Dialer, won't take my kafka.DialerIface. So defining the interface type won't help. Maybe it should be called out very explicitly that this approach only works for a small subset of go librarys that expose interfaces, right?