globalsign / mgo

The MongoDB driver for Go
Other
1.97k stars 230 forks source link

How to mock mgo.Query? #228

Closed bunyk closed 6 years ago

bunyk commented 6 years ago

I have similar issue similar to this: https://github.com/stretchr/testify/issues/418

I have a function that receives collection and calls Find(), so I write following interface for its' parameters:

type MongoCollection interface {
    Find(interface{}) *mgo.Query
}

How I could now construct *mgo.Query in unittests in such a way that query.Iter() gives me iterator with the query results I want?

Or should I write wrapper package for accessing my db, and mock it like advised here: https://stackoverflow.com/questions/46244007/mocking-mgo-chaining-functions ?

timvaillancourt commented 6 years ago

Hi @bunyk, Percona maintains a set of interfaces to do what you describe here: https://github.com/percona/pmgo.

I believe we only support gopkg.in/mgo.v2 but we hope to add/move to using globalsign/mgo in the future, any help is appreciated

eminano commented 6 years ago

Hi @bunyk,

I assume this issue is now resolved thanks to @timvaillancourt's suggestion. I'll close it, but feel free to reopen if there are any further questions.

Thanks, Esther