Hi All I am trying to create a test cases for my gin gonic framework where I am using Gorm and ORM but I am getting errors like this:
[31m2024/07/03 08:48:19 [Recovery] 2024/07/03 - 08:48:19 panic recovered:
runtime error: invalid memory address or nil pointer dereference
C:/Program Files/Go/src/runtime/panic.go:261 (0xc03aa6)
panicmem: panic(memoryError)
C:/Program Files/Go/src/runtime/signal_windows.go:401 (0xc03a76)
sigpanic: panicmem()
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/gorm.io/[email protected]/gorm.go:399 (0xde0dd2)
(*DB).getInstance: if db.clone > 0 {
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/gorm.io/[email protected]/chainable_api.go:201 (0xddb9f5)
(*DB).Where: tx = db.getInstance()
c:/Users/ASUS.LAPTOP-K9MTN7MP/Desktop/Shaffra/CMS/usecases/user_usecase.go:36 (0x1077536)
(*UserUsecase).Login: err := infrastructure.DB.Where("email = ?", email).First(&user).Error
c:/Users/ASUS.LAPTOP-K9MTN7MP/Desktop/Shaffra/CMS/interfaces/organization_handler.go:148 (0x115a07e)
(*OrganizationHandler).Login: user, err := h.UserUsecase.Login(input.Email, input.Password)
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:185 (0x1152879)
(*Context).Next: c.handlers[c.index](c)
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/recovery.go:102 (0x1152867)
CustomRecoveryWithWriter.func1: c.Next()
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:185 (0x11519a4)
(*Context).Next: c.handlers[c.index](c)
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/logger.go:249 (0x115198b)
LoggerWithConfig.func1: c.Next()
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/context.go:185 (0x1150dd1)
(*Context).Next: c.handlers[c.index](c)
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:633 (0x1150840)
(*Engine).handleHTTPRequest: c.Next()
C:/Users/ASUS.LAPTOP-K9MTN7MP/go/pkg/mod/github.com/gin-gonic/[email protected]/gin.go:589 (0x11504d1)
(*Engine).ServeHTTP: engine.handleHTTPRequest(c)
c:/Users/ASUS.LAPTOP-K9MTN7MP/Desktop/Shaffra/CMS/tests/organization_test.go:95 (0x115bf07)
TestRegisterAndLogin: router.ServeHTTP(w, req)
C:/Program Files/Go/src/testing/testing.go:1689 (0xcc893a)
tRunner: fn(t)
C:/Program Files/Go/src/runtime/asm_amd64.s:1695 (0xc25de0)
goexit: BYTE $0x90 // NOP
[0m
I am using this code to run this test cases:
func TestRegisterAndLogin(t *testing.T) {
router := setupRouter()
// Test registration
registerPayload := map[string]string{
"email": "[email protected]",
"password": "password",
}
registerBody, _ := json.Marshal(registerPayload)
req, _ := http.NewRequest("POST", "/register", bytes.NewBuffer(registerBody))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()
router.ServeHTTP(w, req)
t.Logf("registerBody: %s", bytes.NewBuffer(registerBody))
if w.Code != http.StatusOK {
// Log the response body for debugging
body, _ := ioutil.ReadAll(w.Body)
t.Logf("Response body: %s", body)
}
assert.Equal(t, http.StatusOK, w.Code) // Assuming successful registration returns 200
// Test login
loginPayload := map[string]string{
"email": "[email protected]",
"password": "password",
}
loginBody, _ := json.Marshal(loginPayload)
w = httptest.NewRecorder()
req, _ = http.NewRequest("POST", "/login", bytes.NewBuffer(loginBody))
req.Header.Set("Content-Type", "application/json")
router.ServeHTTP(w, req)
if w.Code != http.StatusOK {
// Log the response body for debugging
body, _ := ioutil.ReadAll(w.Body)
t.Logf("Response body: %s", body)
}
assert.Equal(t, http.StatusOK, w.Code) // Assuming successful login returns 200
}
My router function is:
func setupRouter() *gin.Engine {
gin.SetMode(gin.TestMode)
router := gin.Default()
// Load test configuration
cfg := config.LoadTestConfig()
// Initialize the test database
infrastructure.InitTestDatabase(cfg)
organizationUsecase := &usecases.OrganizationUsecase{DB: infrastructure.TestDB}
organizationHandler := &interfaces.OrganizationHandler{Usecase: organizationUsecase}
authMiddleware := MockJWTAuth()
routes.OrganizationRoutes(router, organizationHandler, authMiddleware)
return router
}
My DB connection and everything works fine my code is also running fine but I am unable to run this test case I am using winodws os and vs code as editor