Re:Mocking time and testing event loops in Go
by EdSchouten on Tuesday May 26, 2020 @12:11AM
(#23307338)
Though that is true, remember that Go doesn’t support co/contravariance on interface types.
Defining interfaces around existing types works, but quickly falls apart as soon as methods return concrete types. In those cases you need to write more complex wrappers, which is often not worth the hassle.
In general, libraries should offer structs, and you should declare your own interfaces with the subset of methods that you require.
This is somewhat messy here because the timer channel is accessed by a field rather than a method, but you could write a struct that embeds time.Timer and adds C() as a method. Then both your wrapped time.Timer and your mock/fake could fulfill your interface.