Skip to content

How to go about testing queries #45

Closed Answered by stephenafamo
miniscruff asked this question in Q&A
Discussion options

You must be logged in to vote

bob.Executor is just an interface which is easy to mock. Any existing package to mock a database/sql connection can also be used with Bob. For example, if using DATA-DOG/go-sqlmock:

func TestCar_GetReturnsCar(t *testing.T) {
    ctx := context.Background()

    db, mock, err := sqlmock.New()
    if err != nil {
        t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
    }
    defer db.Close()

    exec := bob.NewDB(db) // This is a valid bob executor

    // Do the tests
}

However, with factories, it becomes easier to test your code directly against a DB. You're already quite close, my preferred method is to use DATA-DOG/go-txdb to insert these entr…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@miniscruff
Comment options

Answer selected by stephenafamo
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants