I'm new to Go and doing a little simple project + the habit of testing the habit, but I'm having problems setting up the test using the layout. In particular, when setting up a mock object
sample /sample.go
package sample
import (
"fmt"
"net/http"
)
func GetResponse(path, employeeID string) string {
url := fmt.Sprintf("http://example.com/%s/%s", path, employeeID)
return value
}
func Process(path, employeeID string) string {
return GetResponse(path, employeeID)
}
sample / sample _test.go
package sample_test
import (
"testing"
"github.com/example/sample" // want to mock some method on this package
)
func TestProcess(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
sample.MOCK()SetController(ctrl)
sample.EXPECT().GetResponse("path", "employeeID").Return("abc")
v := sample.GetResponse("path", "employeeID")
fmt.Println(v)
}
Every time I run this with
go test
I always get an error
undefined: sample.MOCK
undefined: sample.EXPECT
Can anyone point out what I'm doing wrong? Do I need to initialize the layout first? before taunting this method?
If I make GetResponse private [getResponse], can I fix it correctly?
Appreciate all the help .. Thanks!
source
share