-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Mocking sqlx pkg for testing #204
Comments
It should be trivial to mock I'm not sure how how you'd mock other sql functions; presumably you'd have to resort to DI. |
Yes, it was pretty straightforward. mockDB, mock, err := sqlmock.New()
defer mockDB.Close()
sqlxDB = sqlx.NewDb(mockDB,"sqlmock") Later on I used the sqlmock function: mock.ExpectExec("INSERT INTO baskets").WillReturnResult(sqlmock.NewResult(newID, 1)) for the sqlx query: sqlxDB.Exec("INSERT INTO baskets (user_id, name, created_at, updated_at) VALUES (?, ?, ?, ?)", basket.UserID, basket.Name, timeNow, timeNow) and I used the sqlmock function: rows := sqlmock.NewRows([]string{"id", "user_id", "name", "created_at", "updated_at"}).
AddRow(1, userID, name, timeNow, timeNow)
mock.ExpectPrepare("^SELECT (.+) FROM baskets WHERE").ExpectQuery().WithArgs(userID).WillReturnRows(rows) for the sqlx query: sqlxDB.PrepareNamed("SELECT id, user_id , name, created_at, updated_at FROM baskets WHERE user_id = :user_id") |
how to prepare insert query? |
@sim4life Is |
How could I mock I am getting error from sqlmock |
Hi @sim4life @nachogoca Were you guys able to mock with Queryx?
but keep on getting query mismatch. Do you have an example for above case? Also I do not see any issues with SELECT (+.) FROM Table. |
Yes |
Yes, I managed to mock SQLMock .. but I'm NOT sure about QueryX. |
Is there a straightforward way to mock sqlx package for unit testing?
I'm thinking of creating a mock wrapper of sqlx functions and call Data-Dog sqlmock pkg from those functions.
Is there a better approach?
The text was updated successfully, but these errors were encountered: