DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
5.95k stars 404 forks source link

test a call to a procedure #327

Closed Kowiste closed 1 month ago

Kowiste commented 7 months ago

I have to test one function where there is a call to a procedure like this

    if err = DB.WithContext(ctx).Raw("call x_get_number_by_id( ?)", id).Scan(&n).Error; err != nil {
        return n, err
    }

in the test i'm trying this

mockDB.ExpectExec("call x_get_number_by_id( ?)").WithArgs(2).WillReturnError(errors.New("test"))

and I have this error

2023/11/23 09:45:45 number.go:96 call to Query 'call x_get_number_by_id( ?)' with args [{Name: Ordinal:1 Value:2}], was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:
  - matches sql: 'call x_get_number_by_id( ?)'
  - is with arguments:
    0 - 2
  - should return error: 
[0.123ms] [rows:-] call x_get_number_by_id( 2)

There is not any other previous call to a database

diegommm commented 1 month ago

Hi @Kowiste! It appears you are not using database/sql directly, could you clarify what database wrapper library and version are you using? By what you are showing, it appears that your wrapper library is calling Query or QueryRow instead of Exec as you were expecting in your test, so I would suggest you start by using mockDB.ExpectQuery instead of mockDB.ExpectExec.

Closing as this is not an issue in sqlmock, but feel free to keep commenting if it's not solved and we may be able to help you with your code.