A simple solution to this would be:
type DBClientIntf interface {
WriteObject(ObjType)
}
func WriteObject(obj ObjType,cli DBClientIntf) {
cli.writeObject(obj)
}
Or, you can have a factory:
func WriteObject(obj ObjType,cli func() DBClientIntf) {
cli().writeObject(obj)
}
Or, a global factory:
var getDBClient=func() DBClientIntf {
return DBClient{}
}
func WriteObject(obj ObjType) {
getDBClient().writeObject(obj)
}
and set the global factory to a function that returns a mocked client:
func TestWriteObject(t *testing.T) {
getDBClient=func() DBClient {
return mockDBClient{}
}
...
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…