Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Golang interfaces & mocking

I'm having hard times writing unit tests in Go due to external libraries which don't expose an interface (therefore not mockable) but only pure functions. Even big ones like Google don't, so I'm wondering whether my approach is good enough. Wouldn't be good practice for libraries to provide interfaces instead of packages with only functions in order to let the user mock them?

The solution I came up with until now is wrap these packages with an interface's implementation but that seem like too much work.

I come with an example. My function could look like this

func AnyFunction() error {
    sess := session.Get("blabla")
    // logic in here...
}

where session is an imported package that returns a struct. I can't mock the package session. For this case I'm going to write a SessionInterface with an implementation, which internally calls session.

Ex:

type SessionInterface interface {
    Get(s string) Session
}

type mySessionImpl struct {}
func (me *mySessionImpl) Get(s string) Session {
  return session.Get(s)
}

For my tests now I can mock the SessionInterface and inject that one into my code

like image 578
ianaz Avatar asked Mar 11 '19 15:03

ianaz


People also ask

What are Golang interfaces?

Go language interfaces are different from other languages. In Go language, the interface is a custom type that is used to specify a set of one or more method signatures and the interface is abstract, so you are not allowed to create an instance of the interface.

What are interfaces used for in Go?

Interfaces allow us to not repeat code. We can use interfaces to pass multiple structs into the same function where we want the same behavior.

Can a Golang interface have variables?

Since the interface is a type just like a struct, we can create a variable of its type. In the above case, we can create a variable s of type interface Shape .


1 Answers

You're doing the right thing here, and this was a conscious decision on the part of the language designers.

The Go philosophy is that your code should "own" those interfaces, not the library. In languages like C# and Java, libraries define their interfaces up front, without really knowing what the consumer actually needs. (Maybe they include too many methods, or too few.) In Go, because the consumer effectively "owns" the interfaces, you're empowered to specify which methods actually need to be present in a minimal interface, and changes in your program requirements mean that you can also change the interface.

Now in this particular case it might seem strange to create an adapter in front of a function for testability, but consider the alternative: if session.Get() were a method of an interface or struct, instead of a function, it would force all library consumers to instantiate a dummy object in order to call the method. Not everyone is going to fake it out---it's easier for them to say that the consumers who want to (like you) are empowered to write adapters, and those that don't can blissfully ignore them.

like image 152
Keith Ripley Avatar answered Nov 16 '22 02:11

Keith Ripley