Closed Osiris1975 closed 4 years ago
Hi, Instead of trying to escape the query, you could just use an equals query string matcher.
db, mock, err := sqlmock.New(sqlmock.QueryMatcherOption(sqlmock.QueryMatcherEqual))
You can even create your own matcher to print which column did not match.
I personally used regex matching to a level like matching just SELECT
part of query if I knew that there was only one select being tested. Asserting a query string will require to update all tests if you would just add sorting for instance.
@l3pp4rd thanks very much for responding. I have made the line change you suggested, and that combined with changing my query to:
s.mock.ExpectQuery("SELECT * FROM \"fields\" WHERE (\"fields\".\"id\" = $1) ORDER BY \"fields\".\"id\" ASC LIMIT 1").
WithArgs(id).
WillReturnRows(sqlmock.NewRows(row).
AddRow(id, partnerId, qualifiedName, description, dataType, createdAt, createdBy, updatedAt, updatedBy))
worked, thanks very much!
Hi,
So I'm so happy to have found this library, but I've spent two days now trying to get this query to work so I can mock. I've seen various other posts here about properly escaping my query, but I still can't seem to get it working. Here's my error message:
Here's my unit test:
Here's the
Fields
object model from the test:I have tried the following iterations of the query with no success:
and
and also using regexp.QuoteMeta which supposedly does the escaping for you, though I noticed in the output it only put one
/
instead of 2 as I've seen in some other posts here.Also, here's the code for the method under test: