'How to unit test a function with gin context while initializing all dependencies through docker?
I am running my main function ,intializing database ,routers through docker then performing unit test for this particular api (masterController) which is a long code so not pasting it here.The problem that i am facing is that i am initializing all my dependencies while running docker but when i'm testing this function they are not initialized like for example it is giving panic on line 31 where i'm using logger which was initialized at the time of running docker and i can't include all my dependencies in my unit test.
func TestMasterController(t *testing.T) {
gin.SetMode(gin.TestMode)
w := httptest.NewRecorder()
ctx, _ := gin.CreateTestContext(w)
ctx.Request = &http.Request{
URL: &url.URL{},
Header: make(http.Header),
}
param := map[string]interface{}{
"some_data":" "
}
MockJsonPost(ctx, param)
MasterController(ctx)
fmt.Println("inside test function")
assert.EqualValues(t, http.StatusOK, w.Code)
}
func MockJsonPost(c *gin.Context, content interface{}) {
c.Request.Method = "POST" // or PUT
c.Request.Header.Set("Content-Type", "application/json")
jsonbytes, err := json.Marshal(content)
if err != nil {
panic(err)
}
// the request body must be an io.ReadCloser
// the bytes buffer though doesn't implement io.Closer,
// so you wrap it in a no-op closer
c.Request.Body = io.NopCloser(bytes.NewBuffer(jsonbytes))
}
func MasterController(c *gin.Context) {
logger.Primary.Trace("enter MasterController")
defer logger.Primary.Trace("exit MasterController")
var reqBody dtos.EntryExitRequestBody
response := dtos.EntryExitResponseBody{
Status: modelConstant.ControllerResponseStatusFailure,
Event: modelConstant.UnsuccessfulDatabaseFetch,
}
defer logger.Primary.Debugf("responseBody MasterController: %+v", &response)
if err := c.ShouldBindJSON(&reqBody); err != nil {
logger.Primary.Errorf("error parsing reqBody: %v", err)
response.Event = modelConstant.UnsuccessfulParse
c.JSON(http.StatusBadRequest, response)
go sentry.CaptureException(fmt.Errorf("error parsing request body, error: %v", err))
return
}
logger.Primary.Debugf("requestBody MasterController: %+v", reqBody)
//more code
}
In mastercontroller first of all i am doing should bind json then using that data for further process. getting panic -- Output --
=== RUN TestMasterController
--- FAIL: TestMasterController (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x7842b9]
goroutine 26 [running]:
testing.tRunner.func1.2({0xbf7a80, 0x128bcf0})
/usr/local/go/src/testing/testing.go:1389 +0x24e
testing.tRunner.func1()
/usr/local/go/src/testing/testing.go:1392 +0x39f
panic({0xbf7a80, 0x128bcf0})
/usr/local/go/src/runtime/panic.go:838 +0x207
github.com/sirupsen/logrus.(*Entry).Log(0x0?, 0x0?, {0xc000651ad8?, 0x0?, 0x0?})
/home/lenevo/go/pkg/mod/github.com/sirupsen/[email protected]/entry.go:292 +0x19
github.com/sirupsen/logrus.(*Entry).Trace(...)
/home/lenevo/go/pkg/mod/github.com/sirupsen/[email protected]/entry.go:298
pms/controller/master.MasterController(0xc00011c400)
/home/lenevo/Desktop/parkplus/parkplus-pms/controller/master/master.go:31 +0x7c
pms/controller/master.TestMasterController(0x0?)
/home/lenevo/Desktop/parkplus/parkplus-pms/controller/master/master_test.go:57 +0x485
testing.tRunner(0xc0000b6680, 0xd20c50)
/usr/local/go/src/testing/testing.go:1439 +0x102
created by testing.(*T).Run
/usr/local/go/src/testing/testing.go:1486 +0x35f
exit status 2
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
