【发布时间】:2020-01-03 13:03:54
【问题描述】:
我有一个下面的函数 tryGet() 来进行单元测试:
type config struct {
Key string `json:"key"`
Client todo.Client `json:"client"`
}
var instance *config
func (c *config) tryGet() error {
client := &http.Client{}
tClient := Client{"http://url", client}
configValues := config{"Key", tClient}
Instance := &configValues
err := Instance.Client.perform("GET", header)
return nil
}
// 名为“todo”的包中的外部库具有以下结构和函数
package todo
type Client struct {
BaseURL string
HTTPClient *http.Client
}
func (client *Client) perform() error {
return nil
}
我发现很难模拟客户端并在外部包 todo 中执行
【问题讨论】:
-
只执行返回 nil 吗?
-
@edkeveked 否。它做了一些操作,如果成功则返回 nil else error
-
您能否在您的问题中添加您在代码中的调用方式?
-
@edkeveked 我已经更新了关于如何调用 perform 的代码