Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to mock an update with returning #328

Open
agustin-del-pino opened this issue Dec 1, 2023 · 1 comment
Open

How to mock an update with returning #328

agustin-del-pino opened this issue Dec 1, 2023 · 1 comment

Comments

@agustin-del-pino
Copy link

Hi!,

I've the following update:

ret := r.db.Exec(regexp.QuoteMeta("UPDATE $1 SET Counter = Counter + $2 RETURNING Counter + 1"), "COUTERS", n)
var c int
ret.Scan(&c)

The test has:

m.ExpectQuery(
		regexp.QuoteMeta(
			`UPDATE $1 SET Counter = Counter + $2 RETURNING Counter + 1`,
		)).WithArgs("COUNTERS", 3).
		WillReturnRows(sqlmock.NewRows([]string{""}).AddRow(1)

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:

  • matches sql: 'UPDATE $1 SET Counter = Counter + $2 RETURNING Counter + 1)'
  • is with arguments:
    0 - COUNTERS
    1 - 3
  • should return rows:
    row 0 - [1]

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?.

@IvoGoman
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants