When attempting to match a query in a Gorm test using sqlmock, the following error is encountered:
(path/to/my/project/database.go:263) [2020-01-08 10:29:40] Query: could not match actual sql: "SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1" with expected regexp "SELECT * FROM "storage_pools" WHERE "storage_pools"."deleted_at" IS NULL AND ((poolid = ?)) ORDER BY "storage_pools"."id" ASC LIMIT 1"
The issue stems from the use of regexp as the sqlmock query matcher. By default, sqlmock uses a strict equality query matcher, which requires an exact match between the actual query and the expected pattern.
To solve this, use the QueryMatcherEqual option to replace the default query matcher:
QueryMatcherOption(sqlmock.QueryMatcherEqual)
The above is the detailed content of Why is Sqlmock Failing to Match My Query Despite Identical Output in the Log?. For more information, please follow other related articles on the PHP Chinese website!