0

I'm setting up testing in Go. I use go-sqlmock to test mysql connection. Now I try to test mysql insert logic. But the error occurs.
I want to know how to resolve this error.

server side: golang
db: mysql
web framework: gin

dao.go

func PostDao(db *sql.DB, article util.Article, uu string) {
    ins, err := db.Prepare("INSERT INTO articles(uuid, title,content) VALUES(?,?,?)")
    if err != nil {
        log.Fatal(err)
    }
    ins.Exec(uu, article.TITLE, article.CONTENT)
}

dao_test.go

func TestPostArticleDao(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()

    mock.ExpectExec("^INSERT INTO articles*").
        WithArgs("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "test", "test").
        WillReturnResult(sqlmock.NewResult(1, 1))

    article := util.Article{
        ID:      1,
        TITLE:   "test",
        CONTENT: "test",
    }

    PostDao(db, article, "bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c")

    if err := mock.ExpectationsWereMet(); err != nil {
        t.Errorf("there were unfulfilled expections: %s", err)
    }
}

I expect go test -v runs without error.
But the actual is not.
Here is the error.

=== RUN   TestPostArticleDao
2019/08/31 00:08:11 call to Prepare statement with query 'INSERT INTO articles(uuid, title,content) VALUES(?,?,?)', was not expected, next expectation is: ExpectedExec => expecting Exec or ExecContext which:
  - matches sql: 'INSERT INTO articles(uuid, title,content) VALUES(?,?,?)'
  - is with arguments:
    0 - bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c
    1 - test
    2 - test
  - should return Result having:
      LastInsertId: 1
      RowsAffected: 1
exit status 1
FAIL    article/api/dao 0.022s
3
  • The error is pretty explicit. It expected an Exec, but got a Pepare. What part do you need help with ? Commented Aug 30, 2019 at 16:02
  • @Flimzy I want to resolve this error and make this test success. Commented Aug 30, 2019 at 16:04
  • Obviously you want to fix it. The question is what help do you need beyond reading the error message? Commented Aug 30, 2019 at 16:40

2 Answers 2

2

As @Flimzy suggested, it needs to set ExpectPrepare first.
So I changed dao_test.go in this way:

    prep := mock.ExpectPrepare("^INSERT INTO articles*")

    prep.ExpectExec().
        WithArgs("bea1b24d-0627-4ea0-aa2b-8af4c6c2a41c", "test", "test").
        WillReturnResult(sqlmock.NewResult(1, 1))
Sign up to request clarification or add additional context in comments.

Comments

0

In my case it worked without asterix:

mock.ExpectExec("INSERT INTO `mytable`").WithArgs(mockdbutils.AnyTime{}, mockdbutils.AnyTime{}, nil, 4455,false).WillReturnResult(sqlmock.NewResult(int64(4455), 1))
    mock.ExpectCommit()

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.