DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.02k stars 406 forks source link

Question: how to change return value of some methods? #258

Closed resotto closed 2 years ago

resotto commented 3 years ago

Hi. I want to change return value (error) of some methods such as (sql.DB).Query() when they are called in tests.

For example, some DBs return error when (sql.DB).Query() is called and other DBs don't return error when it is called in tests.

Then, I wrote WillReturnError() in order to do that but this doesn't work (which still returns error).

d, m, _ := sqlmock.New()
m.ExpectQuery("SELECT id, name from users where id = ?").WillReturnError(nil)

(omitted)

_, err := d.Query("SELECT id, name from users where id = ?")

Similarly, I would like to change return values of QueryContext, QueryRow, QueryRowContext, Begin, BeginTx, Exec, ExecContext, Prepare, PrepareContext in tests.

How should I fix this? Thank you.