亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何存根對 GitHub 的調用以進行測試?

如何存根對 GitHub 的調用以進行測試?

Go
小唯快跑啊 2022-10-04 16:36:58
我需要使用go-github創建一個拉取請求注釋,我的代碼可以工作,但現在我想為它編寫測試(是的,我知道測試應該放在第一位),這樣我就不會在測試期間實際調用真正的GitHub服務。我已經閱讀了3篇關于golang茬蟄和嘲笑的博客,但是,作為golang的新手,盡管對go-github問題進行了討論,但我還是有點迷茫。例如,我編寫了以下函數:// this is my functionfunc GetClient(token string, url string) (*github.Client, context.Context, error) {    ctx := context.Background()    ts := oauth2.StaticTokenSource(        &oauth2.Token{AccessToken: token},    )    tc := oauth2.NewClient(ctx, ts)    client, err := github.NewEnterpriseClient(url, url, tc)    if err != nil {        fmt.Printf("error creating github client: %q", err)        return nil, nil, err    }    return client, ctx, nil}我怎么能存根呢?同樣,我有這個:func GetPRComments(ctx context.Context, client *github.Client) ([]*github.IssueComment, *github.Response, error)  {    opts := &github.IssueListCommentsOptions{        ListOptions: github.ListOptions{            Page:    1,            PerPage: 30,        },    }    githubPrNumber, err := strconv.Atoi(os.Getenv("GITHUB_PR_NUMBER"))    if err != nil || githubPrNumber == 0 {      panic("error: GITHUB_PR_NUMBER is not numeric or empty")    }    // use Issues API for PR comments since GitHub docs say "This may seem counterintuitive... but a...Pull Request is just an Issue with code"    comments, response, err := client.Issues.ListComments(          ctx,          os.Getenv("GITHUB_OWNER"),          os.Getenv("GITHUB_REPO"),          githubPrNumber,          opts)    if err != nil {        return nil, nil, err    }    return comments, response, nil}我應該如何存根?我的想法是也許通過首先創建自己的結構來使用依賴注入,但我不確定如何,所以目前我有這個:func TestGetClient(t *testing.T) {    client, ctx, err := GetClient(os.Getenv("GITHUB_TOKEN"), "https://example.com/api/v3/")    c, r, err := GetPRComments(ctx, client)    ...}
查看完整描述

1 回答

?
小怪獸愛吃肉

TA貢獻1852條經驗 獲得超1個贊

我會從一個界面開始:


type ClientProvider interface {

  GetClient(token string, url string) (*github.Client, context.Context, error)

}

測試需要調用的單元時,請確保依賴于接口:GetClientClientProvider


func YourFunctionThatNeedsAClient(clientProvider ClientProvider) error {

  // build you token and url


  // get a github client

  client, ctx, err := clientProvider.GetClient(token, url)

  

  // do stuff with the client


  return nil

}

現在,在您的測試中,您可以構造如下存根:


// A mock/stub client provider, set the client func in your test to mock the behavior

type MockClientProvider struct {

  GetClientFunc func(string, string) (*github.Client, context.Context, error)

}


// This will establish for the compiler that MockClientProvider can be used as the interface you created

func (provider *MockClientProvider) GetClient(token string, url string) (*github.Client, context.Context, error) {

  return provider.GetClientFunc(token, url)

}


// Your unit test

func TestYourFunctionThatNeedsAClient(t *testing.T) {

  mockGetClientFunc := func(token string, url string) (*github.Client, context.Context, error) {

    // do your setup here

    return nil, nil, nil // return something better than this

  }


  mockClientProvider := &MockClientProvider{GetClientFunc: mockGetClientFunc}


  // Run your test

  err := YourFunctionThatNeedsAClient(mockClientProvider)


  // Assert your result

}

這些想法不是我自己的,我從我之前的人那里借來的。Mat Ryer在一個關于“慣用語golang”的精彩視頻中提出了這個(和其他想法)。


如果要存根 github 客戶端本身,可以使用類似的方法(如果是 github)??蛻舳耸且粋€結構,你可以用一個接口來隱藏它。如果它已經是一個接口,則上述方法直接起作用。


查看完整回答
反對 回復 2022-10-04
  • 1 回答
  • 0 關注
  • 106 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號