title: How to correctly set Mock Row and Query for go-sqlmock
body: <p>I'm setting up testing in <code>golang</code>.<br>
  I use <code>go-sqlmock</code> to test mysql connection.<br>
  But <code>sqlmock.NewRows</code> and <code>mock.ExpectQuery</code> does not work well with error.<br>
  I want to know how to resolve this error.<br></p>
  
  <pre><code>server side: golang
  db: mysql
  web framework: gin
  </code></pre>
  
  <p>dao.go</p>
  
  <pre class="lang-golang prettyprint-override"><code>func GetSingleArticleDao(c *gin.Context, db *sql.DB) (util.Article, *sql.Rows) {
      id := c.Params.ByName("id")
      article := util.Article{}
      errArticle := db.QueryRow("SELECT * FROM articles WHERE id = ?", id).Scan(&amp;article.ID, &amp;article.UUID, &amp;article.TITLE, &amp;article.CONTENT)
      if errArticle != nil {
          panic(errArticle.Error())
      }
      rows, errImage := db.Query("SELECT image_name FROM images WHERE article_uuid  = ?", article.UUID)
      if errImage != nil {
          panic(errImage.Error())
      }
  
      return article, rows
  }
  </code></pre>
  
  <p>dao_test.go</p>
  
  <pre class="lang-golang prettyprint-override"><code>func TestGetSingleArticleDao(t *testing.T) {
      db, mock, err := sqlmock.New()
  
      if err != nil {
          t.Fatalf("an error '%s' was not expected when opening a stub database connection", err)
      }
  
      defer db.Close()
  
      articleMockRows := sqlmock.NewRows([]string{"id", "uuid", "title", "content"}).
          AddRow("1", "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "test", "test")
  
      mock.ExpectQuery("SELECT (.+) FROM articles where id=\\?").
          WithArgs("1").
          WillReturnRows(articleMockRows)
  
      imageMockRows := sqlmock.NewRows([]string{"article_uuid", "image_name"}).
          AddRow("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "1a90696f-4fe7-48f5-81a5-ca72c129f4b0").
          AddRow("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "3d997272-468f-4b66-91db-00c39f0ef717")
  
      mock.ExpectQuery("^SELECT (.+) FROM images*").
          WithArgs("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c").
          WillReturnRows(imageMockRows)
  
      resp := httptest.NewRecorder()
      gin.SetMode(gin.TestMode)
      ctx, _ := gin.CreateTestContext(resp)
  
      article, imageRows := GetSingleArticleDao(ctx, db)
  
      for imageRows.Next() {
          imageName := util.ImageName{}
          err := imageRows.Scan(&amp;imageName.NAME)
          if err != nil {
              panic(err.Error())
          }
          article.IMAGENAMES = append(article.IMAGENAMES, imageName)
      }
  
      expectedArticle := util.Article{
          ID:      1,
          UUID:    "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c",
          TITLE:   "test",
          CONTENT: "test",
      }
  
      imageName1 := util.ImageName{
          NAME: "1a90696f-4fe7-48f5-81a5-ca72c129f4b0",
      }
      expectedArticle.IMAGENAMES = append(expectedArticle.IMAGENAMES, imageName1)
  
      imageName2 := util.ImageName{
          NAME: "3d997272-468f-4b66-91db-00c39f0ef717",
      }
  
      expectedArticle.IMAGENAMES = append(expectedArticle.IMAGENAMES, imageName2)
  
      assert.Equal(t, expectedArticle, article)
  }
  </code></pre>
  
  <p>I expect <code>go test -v</code> runs without error.<br>
  But the actual is not.<br>
  Here is the error.<br></p>
  
  <pre><code>$ go test -v 
  === RUN   TestGetSingleArticleDao
  --- FAIL: TestGetSingleArticleDao (0.00s)
  panic: Query: could not match actual sql: "SELECT * FROM articles WHERE id = ?" with expected regexp "SELECT (.+) FROM articles where id=\?" [recovered]
          panic: Query: could not match actual sql: "SELECT * FROM articles WHERE id = ?" with expected regexp "SELECT (.+) FROM articles where id=\?"
  
  goroutine 34 [running]:
  testing.tRunner.func1(0xc00022c000)
          /usr/local/go/src/testing/testing.go:830 +0x392
  panic(0x165a960, 0xc0001bc4a0)
          /usr/local/go/src/runtime/panic.go:522 +0x1b5
  article/api/dao.GetSingleArticleDao(0xc000214160, 0xc0002180c0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, ...)
          /Users/jpskgc/article/api/dao/dao.go:26 +0x35d
  article/api/dao.TestGetSingleArticleDao(0xc00022c000)
          /Users/jpskgc/article/api/dao/dao_test.go:92 +0x750
  testing.tRunner(0xc00022c000, 0x1788730)
          /usr/local/go/src/testing/testing.go:865 +0xc0
  created by testing.(*T).Run
          /usr/local/go/src/testing/testing.go:916 +0x35a
  exit status 2
  FAIL    article/api/dao 0.030s
  </code></pre>