I have a simple Go function which I want to test:
func root(c echo.Context) error {
return c.String(http.StatusOK, "This is the root!")
}
My tests look like this:
import (
"net/http"
"testing"
"github.com/labstack/echo"
"github.com/stretchr/testify/assert"
)
type Context struct {
echo.Context
}
func TestRoot(t *testing.T) {
c := new(Context)
expectedResult := c.String(http.StatusOK, "This is the root!")
actualResult := c.String(http.StatusOK, "This is the root!")
assert.Equal(t, expectedResult, actualResult, "they should be equal")
}
Now, when I run the test, I am getting this error:
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
panic: runtime error: invalid memory address or nil pointer dereference
I am fairly new to Go and just want to try to make sense of it. I get it that when I address an object which does not exist, I am getting this error. But every variable assignment I am doing has no change to be nil
.
Can somebody point me to a solution? Or tell me how simple unit tests should look like in Go?