基本上我已经开始在Riot Games API的包装上工作了,我正在努力如何测试它。我已经将存储库插入到Travis中,所以它推动它运行go test
,但我不知道如何去测试它,因为请求所需的API_KEY每天都会更改,我无法自动重新生成它,我会如果我直接测试端点,则每天手动添加它。我会如何测试这种方法?
所以我想知道是否有可能嘲笑的反应,但在这种情况下,我猜我需要重构我的代码?
所以我做了一个结构来代表他们的SummonerDTO
type Summoner struct {
ID int64 `json:"id"`
AccountID int64 `json:"accountId"`
ProfileIconID int `json:"profileIconId"`
Name string `json:"name"`
Level int `json:"summonerLevel"`
RevisionDate int64 `json:"revisionDate"`
}
这结构有一个方法:
func (s Summoner) ByName(name string, region string) (summoner *Summoner, err error) {
endpoint := fmt.Sprintf("https://%s.api.riotgames.com/lol/summoner/%s/summoners/by-name/%s", REGIONS[region], VERSION, name)
client := &http.Client{}
req, err := http.NewRequest("GET", endpoint, nil)
if err != nil {
return nil, fmt.Errorf("unable to create new client for request: %v", err)
}
req.Header.Set("X-Riot-Token", API_KEY)
resp, err := client.Do(req)
if err != nil {
return nil, fmt.Errorf("unable to complete request to endpoint: %v", err)
}
defer resp.Body.Close()
if resp.StatusCode != 200 {
return nil, fmt.Errorf("request to api failed with: %v", resp.Status)
}
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("unable to read response body: %v", err)
}
if err := json.Unmarshal([]byte(respBody), &summoner); err != nil {
return nil, fmt.Errorf("unable to unmarshal response body to summoner struct: %v", err)
}
return summoner, nil
}
是它的结构方法,没有一个单一的责任的情况下, ,从某种意义上说,它正在构建端点,引发请求并解析响应。我是否需要重构它才能使其可测试,并且在这种情况下,最佳方法是什么?我应该做一个请求和响应结构,然后测试它们吗?
要澄清API密钥使用率是有限的,需要每天重新生成,Riot Games不允许您使用抓取工具来自动重新生成密钥。我正在使用Travis进行持续集成,所以我想知道是否有方法来模拟请求/响应。
潜在我的方法是错误的,仍然在学习。
希望所有人都能提供某种形式的感觉,如果不是,我们很乐意澄清。
感谢您的回复,对我来说看起来像一个彻底和深思熟虑的过程。在我接受答案之前,会给予其他人一点时间提醒。 – Mikey