go-sqlmock icon indicating copy to clipboard operation
go-sqlmock copied to clipboard

Question: how to change return value of some methods?

Open resotto opened this issue 3 years ago • 0 comments

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.

resotto avatar Mar 15 '21 00:03 resotto