Closed niklod closed 5 months ago
Hi @niklod! Thank you for your detailed submission. However, I think that if you have to manually write every line with rows.AddRow
it would also be fair to ask that you manually enter the number of lines you wrote. Nonetheless, you still have ways to keep the number of rows you entered in your test at hand with sqlmock
, and that could be done by using the (*Rows).AddRows
method. Example:
// test setup
db, mock, err := sqlmock.New()
require.NoError(t , err)
colNames := []string{"id", "enabled", "name"}
rowsData := [][]driver.Value{
{1, true, "someone"},
{2, false, "another person"},
}
// other stuff, like your repository
// setup expectations
rows := mock.NewRows(colNames).AddRows(rowsData...)
mock.ExpectQuery(`SELECT id, enabled, name from users WHERE admin = ?`).WillReturnRows(rows)
// execute and assert
// ... use `db` to get a `results` slice using your repository
assert.Len(t, myResult, len(rowsData))
Also note that you're using sqlmock.NewRows
instead of your sqlmock.Sqlmock
's NewRows
method, which I fixed in the example (read the documentation for more information about this). I think this way you can achieve your goal.
Thank you!
Hello! I think it would be great to make a method Len() for sqlmock.Rows. It will be useful for cases, where we comparing length of resulting rows and mocked rows.
Example:
Currently we have to manually enter the length of the mocked rows like this:
During the development process, tests can be changed and we should manually change this length. Will glad to contribute if you think this is a good idea.