In testing database methods, I created a minimal wrapper over the database/sql package to allow me to test against interfaces instead of the difficult if not impossible to setup concrete classes. But, I get the following error when I try to mock sql.Stmt:
cannot use *sql.Stmt as type IStmt in return argument:
*sql.Stmt does not implement IStmt (wrong type for Query method)
have Query(...interface {}) (*sql.Rows, error)
want Query(...interface {}) (IRows, error)
Here are my interfaces:
type IStmt interface {
Query(args ...interface{}) (IRows, error)
QueryRow(args ...interface{}) IRow
}
type IRows interface {
Columns() ([]string, error)
Next() bool
Close() error
Scan(dest ...interface{}) error
Err() error
}
And here's the problem method:
func (c *DbConnection) Prepare(query string) (IStmt, error) {
return c.conn.Prepare(query)
}
I know that one of the beautiful things about Go is that you can create your own interface and any struct that implements it will automatically "implement" it without having to use the implements
keyword in java or use the semicolon like in C# to subclass. Why isn't it working with this return type? Am I doing something wrong?