Home > Software design >  cannot use mockDB (variable of type *MockDB) as *gorm.DB value in struct literal
cannot use mockDB (variable of type *MockDB) as *gorm.DB value in struct literal

Time:12-29

I created a get function for get exercises from the postgres db. And I wrote mock testing but I got this error from the struct how can I fix it?

I used Handler struct It has *gorm.DB struct.

error:

cannot use mockDB (variable of type *MockDB) as *gorm.DB value in struct literal

// router
package exercises

import (
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
)

type Handlers struct {
    DB *gorm.DB
}

func RegisterRoutes(router *gin.Engine, db *gorm.DB) {
    h := &Handlers{
        DB: db,
    }

    routes := router.Group("/exercises")
    routes.POST("/", h.AddExercise)
    routes.GET("/", h.GetExercises)
    routes.GET("/:id", h.GetExercise)
    routes.PUT("/:id", h.UpdateExercise)
    routes.DELETE("/:id", h.DeleteExercise)
}

// test
package exercises

import (
    "net/http"
    "net/http/httptest"
    "testing"

    "github.com/gin-gonic/gin"
    "github.com/kayraberktuncer/sports-planner/pkg/common/models"
    "github.com/stretchr/testify/mock"
    "gorm.io/gorm"
)

type MockDB struct {
    mock.Mock
}

func (m *MockDB) Find(value interface{}) *gorm.DB {
    args := m.Called(value)
    return args.Get(0).(*gorm.DB)
}

func (m *MockDB) Error() error {
    args := m.Called()
    return args.Error(0)
}

func TestGetExercises(t *testing.T) {
    // Setup mock DB
    mockDB := new(MockDB)
    mockDB.On("Find", &[]models.Exercise{}).Return(mockDB).Once()

    // Setup Gin router
    router := gin.New()
    router.GET("/", func(c *gin.Context) {
        handlers := &Handlers{DB: mockDB} // error
        handlers.GetExercises(c)
    })

    // Perform request
    w := httptest.NewRecorder()
    req, _ := http.NewRequest("GET", "/", nil)
    router.ServeHTTP(w, req)

    // Assert response
    if w.Code != http.StatusOK {
        t.Errorf("Expected status code %d, got %d", http.StatusOK, w.Code)
    }

    // Assert mock DB was called correctly
    mockDB.AssertExpectations(t)
}

I wanted to made mock testing with my handler struct

CodePudding user response:

MockDB and gorm's DB are two different structs and you cannot use them interchangeably. They can be used in the same place if they implement the same interface. For example:

// router
package exercises

import (
    "github.com/gin-gonic/gin"
    "gorm.io/gorm"
)

// this interface will be implemented by gorm.DB struct
type Store interface {
    Create(value interface{}) *gorm.DB
    First(out interface{}, where ...interface{}) *gorm.DB
    Model(value interface{}) *gorm.DB
    Delete(value interface{}, where ...interface{}) *gorm.DB
    Find(out interface{}, where ...interface{}) *gorm.DB
    DB() *sql.DB
    Raw(sql string, values ...interface{}) *gorm.DB
    Exec(sql string, values ...interface{}) *gorm.DB
    Where(query interface{}, args ...interface{}) *gorm.DB
    //other method signatures
}

type Handlers struct {
    DB Store
}

func RegisterRoutes(router *gin.Engine, db Store) {
    h := &Handlers{
        DB: db,
    }

    routes := router.Group("/exercises")
    routes.POST("/", h.AddExercise)
    routes.GET("/", h.GetExercises)
    routes.GET("/:id", h.GetExercise)
    routes.PUT("/:id", h.UpdateExercise)
    routes.DELETE("/:id", h.DeleteExercise)
}

Now, you can pass the *gorm.DB to the RegisterRoutes function in your code. For testing, you can use your MockDB struct if it implements all of the methods from the Store interface.

  • Related