我是 Go 新手,正在做简单的小项目 + 做测试习惯来学习..但是我在使用模拟设置测试时遇到了麻烦。特别是在设置模拟对象
样品/样品。去
package sample
import (
"fmt"
"net/http"
)
func GetResponse(path, employeeID string) string {
url := fmt.Sprintf("http://example.com/%s/%s", path, employeeID)
// made some request here
// then convert resp.Body to string and save it to value
return value
}
func Process(path, employeeID string) string {
return GetResponse(path, employeeID)
}
样本/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)
}
每次我运行这个
go test
我总是收到错误
undefined: sample.MOCK
undefined: sample.EXPECT
任何人都可以指出我做错了什么?我需要先初始化一个模拟对象吗?在嘲笑方法之前?
如果我将 GetResponse 设为私有 [getResponse],我将无法模拟它,对吗?
感谢所有帮助..谢谢!
当年话下
相关分类