dimanche 2 février 2020

Using same expected rows for multiple expected queries returns result just for the first one with sqlmock

I am writing tests with sqlmock in go. I have a list of rows (e.g. myRows) and two different SELECT statements which I want to use myRows as WillReturnRows argument for both of them:

myRows := sqlmock.NewRows([]string{"my_column"}).AddRow(1)
mock.ExpectQuery(firstQuery).WillReturnRows(myRows)
mock.ExpectQuery(secondQuery).WillReturnRows(myRows)

When I use it in the first WillReturnRows, it returns 1 as result. But in the second usage, no rows is returned as result; I mean empty rows returns. I printed out the myRows before and after first call in order to check if it's been consumed or not; the object had no change:

Rows Before:  &{[my_column] [[1]] 0 map[] <nil>}
Rows After:  &{[my_column] [[1]] 0 map[] <nil>}

Aucun commentaire:

Enregistrer un commentaire