我想测试LoadPage和GetData函数。在测试LoadPage时,我想模拟GetData函数,该怎么做?
不想使用类似type PageGetter func(url string) string
之类的东西,并将mock func作为参数传递
package main
import (
"net/http"
"os"
"fmt"
"io/ioutil"
)
func GetData(url string) (*http.Response,error){
return http.Get(url)
}
func LoadPage(url string) {
resp,err := GetData(url)
if err != nil {
os.Exit(0)
}
data,err :=ioutil.ReadAll(resp.Body)
fmt.Println(string(data))
}
func main() {
var url string
fmt.Scanf("%s", &url)
LoadPage(url)
}