Sql 不匹配

作者: ivorytower1026创建于 2024年9月18日更新于 2024年11月6日

// Create a new SQL mock sqlDB, mock, err := sqlmock.New() assert.NoError(t, err) defer sqlDB.Close()

// Create a new GORM DB instance with the mock dialector := MySQL.New(MySQL.Config{ Conn: sqlDB, SkipInitializeWithVersion: true, }) db, err := gorm.Open(dialector, &gorm.Config{}) assert.NoError(t, err)

// Replace the global DB with our mocked DB m.DB = db argService := &CreateArgResponse{} //fixedTime := time.Now() // normal case t.Run("Normal case", func(t *testing.T) { mock.ExpectQuery("SELECT * FROM arg_configs WHERE arg_key = ? ORDER BY arg_configs.id LIMIT ?"). WithArgs("test_key", 1). WillReturnRows(sqlmock.NewRows([]string{"id", "arg_name", "arg_key", "arg_value", "arg_comment"}))

mock.ExpectBegin()
mock.ExpectExec("INSERT INTO `arg_configs` (arg_name, arg_key, arg_value, arg_comment) VALUES (?, ?, ?, ?)").
	WithArgs("Test Arg", "test_key", "test_value", "Test comment")

})

内容来源: DATA-DOG/go-sqlmock