2

I have started learning Golang and I created a simple application with Gin and there are two lines (in main function) that I am not able to test for having a 100% of coverage:

func setupRouter() *gin.Engine {
    fmt.Println("Starting application...")

    router := gin.Default()
    router.GET("/permissions", getPermissionsRoute)
    router.GET("/permission/:id", getPermissionByIDRoute)
    router.POST("/permission", createPermissionRoute)
    router.PUT("/permission", updatePermissionRoute)
    router.DELETE("/permission/:id", deletePermissionByIDRoute)

    return router
}

var callSetupRouter = setupRouter

func main() {
    router := callSetupRouter()
    router.Run(":9090")
}

Since the return type of setupRouter is a pointer to an Engine struct, I did not find a way to mock the function for returning a pointer to an Engine struct with the method Run mocked. Any ideas? Thanks in advance.

druizz90
  • 21
  • 2
  • 2
    If `setupRouter()` returns `*gin.Engine`, the mocker function must also return `*gin.Engine`. If you want to return another type, modify `setupRouter()` to return an interface type having a `Run()` method. – icza Oct 04 '22 at 10:12

0 Answers0