Unit testing mongo-driver for golang

I have a “repository” interface which is independent of any underlying database technology. I have no problem mocking such interface in order to uni test upper layers of my project depending on repository operations.

But now I want to unit test my “mongo” repository interface itself, that is, testing the logic and behavior of every individual function. For that I require a mongo “mock” that allows me to configure what do I want it to return on every test case, but I can’t create such mock because the mongo driver works on package-specific types directly instead of using an interface that I can implement myself :frowning:

When using mysql I have used this package which does precisely that.

I have been thinking of creating an additional wrapper interface for the mongo driver, exposing only the functions that I need to mock for my tests, but seems like a huge task.

So I wonder, how does everyone using the golang mongo-driver implement unit tests for their repository implementations? Any suggestion will be much appreciated! thanks in advance.

Hi, if you still need it, you can use my package GitHub - sv-tools/mongoifc: Interfaces for the MongoDB driver