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 {
    Context
    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
        }
        c.Database.dbConnection.SetMaxOpenConns(50)
    }
    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 https://sqa.stackexchange.com/questions/35200/-testing--for-database-connection-get-in-golang

LEAVE A REPLY

Please enter your comment!
Please enter your name here