DATA-DOG / go-sqlmock

Sql mock driver for golang to test database interactions
Other
6.04k stars 406 forks source link

there were unfulfilled expectations: there is a remaining expectation which was not matched: ExpectedQuery => expecting Query, QueryContext or QueryRow which #189

Closed yumeng110 closed 4 years ago

yumeng110 commented 5 years ago

hi, i hit a problem:the assert is failed if the sql sentence is related with "ws_hot_status = ?", but pass when "ws_hot_status = 1"

func TestGetFeedidList_ReturnLimitData(t *testing.T) {

db, mock, err := sqlmock.New()
if err != nil{
    t.Errorf("An error '%s' was not expected when opening a stub database connection", err)
}
defer db.Close()

patches := gomonkey.ApplyFunc(comm.GetDB, func()(*sql.DB, error){
    return db, nil
})
defer patches.Reset()
columns := []string{"feedid","properties"}
rows := sqlmock.NewRows(columns).
    AddRow("11111", `{"ws_hot_event": "ui test", "ws_hot_source": "","ws_hot_score": 30,"ws_hot_operator": "meng","ws_hot_level": "p1"}`)
tableName := comm.GetHighQualityTable()
mockSql := fmt.Sprintf(`select feedid, properties from %s where ws_hot_status = ? limit ?`, tableName)

mock.ExpectQuery(mockSql).WithArgs(1).WillReturnRows(rows)
fd := new (WeishiHighQualityAutoExitDriver)
fd.Logger = tars.GetDayLogger("ManageFieldsServer", 7)
var AcutalHotInfoList, ActualErr = fd.GetFeedidList()

if err := mock.ExpectationsWereMet(); err != nil {
    t.Errorf("there were unfulfilled expectations: %s", err)
}
assert.Equal(t, 1, len(AcutalHotInfoList))
assert.Equal(t, nil, ActualErr)

}

l3pp4rd commented 4 years ago

by default, sql you are expecting in query is translated to regular expression and it is not matching

tharun208 commented 3 years ago

Hi, I am facing the same problem. My SQL query is simple but still fails. don't know what I missed.

                sqlMock := *sqlMock
        context, _ := gin.CreateTestContext(httpResponseRecorder)
        rows := sqlmock.NewRows([]string{"EMAIL", "PASSWORD"}).
            AddRow("testuser@gmail.com", "password").
            AddRow("testuser@yahoo.com", "password")
        request := request.LoginRequest{
            Email:    "testuser@yahoo.com",
            Password: "password",
        }
        sqlMock.ExpectQuery(`SELECT EMAIL, PASSWORD from USER_MASTER WHERE EMAIL=:EMAIL`).WithArgs("testuser@yahoo.com").WillReturnRows(rows)
        err := sqlMock.ExpectationsWereMet()
        Expect(err).NotTo(HaveOccurred())

        user, err := authenticationRepository.GetUserByEmailID(context, request)
        Expect(err).NotTo(HaveOccurred())
        Expect(user).ShouldNot(BeNil())
        Expect(user.Email).Should(Equal("testuser@yahoo.com"))
CuteSmartTiger commented 2 years ago

in my case ,i miss Grave (symbol) in my mock sql, solve the problem by change table name from {table_name} to {table_name} with grave symbol in mock sql