Closed agustin-del-pino closed 1 month ago
When using ExpecExec(..)
it is possible to set expectations for the result by calling .WillReturnResult(..)
Have a look at https://github.com/DATA-DOG/go-sqlmock#tests-with-sqlmock for a working example.
Hi @agustin-del-pino! It appears you are calling method Exec
but expecting a Query
instead, which may be your sqlmock
issue. But more thoroughly, I think you have a couple of mistakes in your code, some may be due to copy-pasting, but it makes much harder to help you if you don't provide working code (it's fine to redact/change some parts, but make sure at it at least compiles):
regexp.QuoteMeta
in your call to r.db.Exec
(maybe copy-paste error?).r.db.Exec
appears to return a single value, which no Exec
method in database/sql
does (this would give a compilation error). But QueryRow
does, and it also returns a *sql.Row
that has a Scan
method, which none of the returned values of the Exec
methods do. Maybe copy-paste error? Otherwise, if you're not using database/sql
, please specify which wrapper library and version you are using.COUTERS
in your example code, but in the error you show COUNTERS
instead.To answer the question in the title (How to mock an update with returning
): you should either first use Query
/QueryRow
/QueryContext
/QueryRowContext
in your runtime code, then your test use the single ExpectQuery
method.
Hi!,
I've the following update:
The test has:
When I run the test I got this error.
call to ExecQuery 'UPDATE \$1 SET Counter = Counter + \$2 RETURNING Counter + 1)' with args [{Name: Ordinal:1 Value:COUNTERS} {Name: Ordinal:2 Value:3}], was not expected, next expectation is: ExpectedQuery => expecting Query, QueryContext or QueryRow which:
I understand that error is because I'm using Exec. But I don't know how to return the value I need with the Exec mock.
Is there any method for return a value with Exec mock?.