-1

GORM preload is causing this error "all expectations were already fulfilled, call to Query 'SELECT * FROM "self"."xxxxxx" WHERE "xxxxxx"."l7_code" = $1' with args [{Name: Ordinal:1 Value:9920000000054}] was not expected when I am trying to use sqlmock. IF I am not using gorm preload there is no issue with sqlmock.So if we want to mock gorm preload how can we give it in sqlmock AddRow function

3 Answers3

0

After searching for hours ,I found a simple solution of using mock.ExpectQuery to mock GORM 'preload' foreign key sql query execution

  • Can you add a bit more information? Maybe an example? Hitting this case myself and failing to find much resources. – dubplay Mar 31 '22 at 14:35
  • Adding for future readers - it was the order in which I expect the queries. The Preload `SELECT`s come after the `SELECT` of the main model being searched. – dubplay Mar 31 '22 at 16:19
0

I was facing similar issue. You might want to mock the original query first and then mock the preload query. Also, if you have multiple preloads mock them in alphabetical order of table names otherwise it will give an error.

0

I had the same problem. First I wrote expected query for main query and then wrote expected query for preload. And added s.mock.MatchExpectationsInOrder(true). This solved the problem.