There’s a method in the code under test, that simply tries to get , or returns error if unable to.

It, and the structs involved are defined as follows:

type DatabaseContext struct {
    Database DatabaseSt

// //GetInfo Returns the context.
// func (c *DatabaseContext) GetInfo() *Context {
//  return &c.Context
// }

//GetDB Gets the database connection from the connection string.
func (c *DatabaseContext) GetDB() (*sql.DB, *errors.ErrorSt) {
    var errSt *errors.ErrorSt
    if c.Database.dbConnection == nil {
        c.Database.dbConnection, errSt = c.openDB()
        if errSt != nil {
            return nil, errSt
    return c.Database.dbConnection, nil

Fortunately, DatabaseContext implements DatabaseContextIntf, which I want to use for . My instinct is to straight up mock DatabaseContext, but that won’t work because it’s not an interface (in , you can only mock interfaces).

How would I go about testing this, without hitting a real database, which can fail beyond my control (thus creating false fails in the test)?

Source link


Please enter your comment!
Please enter your name here