How to mock mgo?
Nobody has claimed this yet.
Assessment
- Difficulty
- 5/5
- Estimated time
- Over a week
- Newbie friendliness
- 20/100
- Issue type
- Documentation
- Clarity
- Needs clarification
- Activity status
- Stale
- Tech stack
- go
- Domain
- testing-qa
Research direction
The issue provides no repository file or test entry point; begin with the printIDs example and the Collection and Query interfaces around mgo.Collection and mgo.Query. The desired outcome is not specified beyond understanding nested mocks, so there is no defined completion condition.
Written by the indexing model from the issue text.
Description
I have a question which puzzles me quite a bit. Consider the below code snippet, which runs a query against the employee collection, and iterates over the result set and then prints the id of each employee:
func printIDs(c *mgo.Collection) {
query := bson.M{...} // some random query
q := collection.Find(query)
iter := q.Iter()
for iter.Next(&employee) {
hex := employee.Id.Hex()
fmt.Println("id is", hex)
}
}
I know, that in order to be able to pass a mock collection object, I have to change the function signature to use an interface type:
func printIDs(c Collection) {
...
}
Since the printIDs function only uses the Find method on the *mgo.Collection, the new Collection interface will look like this:
type Collection interface {
Find(query interface{}) *mgo.Query
}
But here is where my trouble begins: the printIDs function will use the query object returned from the Find method, therefore I want that returned object to be a mock as well. Just as before, I have to create an interface for *mgo.Query, so now I can use it like this:
c := &MockCollection{}
q := MockQuery{}
c.Mock.On("Find", mock.AnythingOfType("bson.M")).Return(q) // mock collection to return a mock query
printIDs(c)
But now, I have to change the Collection interface's Find method to return this new Query interface type instead of *mgo.Query, which will not work because now the *mgo.Collection is not implementing the Collection interface anymore.
Can anyone throw some light on how to mock this kind of nested structures?
With thanks,
Laszlo
- Dominant language
- Go
- Stars
- 26.2k
- Forks
- 1.9k
- Avg merge
- 2d 4h
- Merged PRs (30d)
- 2
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from stretchr/testify
-
Difficulty 2/5 1-3 hours Newbie friendliness 76/100
-
bug
Difficulty 2/5 1-3 hours Newbie friendliness 88/100
-
bug
Difficulty 1/5 Under an hour Newbie friendliness 88/100
-
enhancement
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
-
internal/cleanup
Difficulty 2/5 1-3 hours Newbie friendliness 68/100
All issues in stretchr/testify
Similar issues
-
optimization optimization:agents-md-curator
Difficulty 2/5 1-3 hours Newbie friendliness 86/100
githubnext/gh-aw-cao#13143 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
blinklabs-io/bursa#904 ·
-
Difficulty 2/5 1-3 hours Newbie friendliness 84/100
yanet-platform/ipfw-go#129 ·
-
bug confmap/provider/googlesecretmanagerprovider needs triage
Difficulty 2/5 1-3 hours Newbie friendliness 72/100
open-telemetry/opentelemetry-collector-contrib#51273 · 2 comments ·
-
bug: AI Gateway client filter lists "Unknown" twice when NULL and literal Unknown clients coexist Openbug
Difficulty 2/5 1-3 hours Newbie friendliness 90/100