I read this article and it has good guides for mocking MongoDB in Go. But there are some problems in Clone()
and Copy()
methods.
I create this interfaces and structs:
type ISession interface {
DB(name string) IDatabase
Close()
Clone() ISession
Copy() ISession
}
type IDatabase interface {
C(name string) ICollection
}
type MongoSession struct {
dbSession *mgo.Session
}
func (s MongoSession) DB(name string) IDatabase {
return &MongoDatabase{Database: s.dbSession.DB(name)}
}
func (s MongoSession) Clone() ISession {
//return session.clone
return s.dbSession.Clone()
}
func (s MongoSession) Copy() ISession {
return s.dbSession.Copy()
}
But I got this error
cannot use s.dbSession.Clone() (type *mgo.Session) as type ISession in return argument: *mgo.Session does not implement ISession (wrong type for Clone method) have Clone() *mgo.Session want Clone() ISession
cannot use s.dbSession.Copy() (type *mgo.Session) as type ISession in return argument: *mgo.Session does not implement ISession (wrong type for Clone method) have Clone() *mgo.Session want Clone() ISession
How can I add Clone()
and Copy()
methods to interface?