* mgo.Database is a pointer to a type, not an interface, you cannot make fun of it.
As in other languages, you need to provide a level of indirection so that you can provide a real object in production, but a layout for testing. So, your first step is to extract the interface that your myFunc uses (what methods it calls), then you can provide * mgo.Database during production and your layout (manual layout or using some kind of mocking structure) for testing.
This free chapter of an example from the book “The Art of Unit Testing” explains the steps you need to follow on page 52 (Chapter 3, “Using Stubs to Break Dependencies” - “3.3 Determining How Easy It Is to Test LogAnalyzer”):
http://www.manning.com/osherove/SampleChapter3.pdf
given that in Go a type implements an interface by simply implementing interface methods - it is even simpler than in other languages ​​(e.g. C #)
so the answer is what you said
write an interface that has all the same methods as mgo.Database and pass a “mocked” object that uses the interface, and then create an object that uses the interface and passes calls through mgo's (similar to ORM), but it looks like a lot of coding .
except that you don't need to create an object that uses the interface and passes calls through to mgo library (similar to an ORM) , because * mgo.Database will implicitly satisfy your interface. So this is not much coding.
Kluyg source share