Mocking mgo chaining functions

跟風遠走 提交于 2019-12-11 09:05:53

问题


While testing some of my codebase I found it useful to mock out objects from "gopkg.in/mgo.v2" with interfaces. I'm running into a problem where mgo.Query does not implement my interface Query.

import mgo "gopkg.in/mgo.v2"

type Collection interface {
    FindId(interface{}) Query
    Find(interface{}) Query
    UpdateId(interface{}, interface{}) error
    Update(interface{}, interface{}) error
    UpsertId(interface{}, interface{}) (interface{}, error)
    Insert(...interface{}) error
    RemoveId(interface{}) error
}

type Query interface {
    One(interface{}) error
    All(interface{}) error
    Select(interface{}) Query
}

var _ Query = (*mgo.Query)(nil)

The Query cast kicks up the error:

cannot use (*mgo.Query)(nil) (type *mgo.Query) as type Query in assignment:
    *mgo.Query does not implement Query (wrong type for Select method)
        have Select(interface {}) *mgo.Query
        want Select(interface {}) Query

Is this a problem with chain functions not being able to be defined in an interface? I'm not sure how to make a Select header that will match the mgo implementation.


回答1:


The function signatures are different, which is why you're running into a compilation error. Your interface version of Select returns your Query type, while mgo's Select returns *mgo.Query, which is a different type. Even if that type does implement your interface, the function signatures are still different. You will need to add another layer to this where you are able to wrap the mgo package.

type MgoQuery struct {
    *mgo.Query
}

func (q *MgoQuery) Select(selector interface{}) Query {
    return q.Query.Select(selector)
}

...


来源:https://stackoverflow.com/questions/46244007/mocking-mgo-chaining-functions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!