douduikai0562 2018-11-02 22:47
浏览 10
已采纳

如何将这个api提取到接口,以便可以在测试中模拟它?

So currently I have a http handler that looks like:

type App struct {

}
func (a *App) someHandler(w http.ResponseWriter, r *http.Request) {
        var api = slack.New("TOKEN")
        users = api.GetUsers()
}

I want to create an interface for this slack.New("...") call so that in my tests the api doesn't make network requests to slack.

How can I mock this New call?

The call New("TOKEN") returns a *Client, see the link below:

func New(token string, options ...Option) *Client {
    s := &Client{
        token:      token,
        httpclient: &http.Client{},
        log:        log.New(os.Stderr, "nlopes/slack", log.LstdFlags|log.Lshortfile),
    }

    for _, opt := range options {
        opt(s)
    }

    return s
}

https://github.com/nlopes/slack/blob/0f8db5050731c50359e319cf253af5b9997a2b1e/slack.go#L84

I haven't used interfaces that much so not sure if this can't be put in a interface since the call to New is like a constructor?

  • 写回答

1条回答 默认 最新

  • dongxing9219 2018-11-03 06:40
    关注

    You can't mock the slack.New call itself, instead you have to create a mock that behaves like this api object. To do this put the api onto the App struct but as an interface:

    type SlackClient interface {
        GetUsers() []string
    }
    
    type App struct {
        api SlackClient
    }
    
    func (a *App) someHandler(w http.ResponseWriter, r *http.Request) { 
        users = a.api.GetUsers() 
    }
    

    You then have to move the call to slack.New into whatever constructs the App (for instance your main function or a NewApp constructor function):

    app = App{api: slack.New("TOKEN")}
    

    The *Client this returns has a GetUsers method on it so will match the interface we have defined.

    The test then does something similar with a mock:

    type mockSlackClient struct {
    
    }
    
    func (m *mockSlackClient) GetUsers() []string {
        return nil
    }
    
    func TestSomeHandler(t *testing.T) {
        appToTest := App{api: &mockSlackClient{})
    
        appToTest.someHandler(httptest.NewRecorder(), nil)
    }
    

    Again as the *mockSlackClient has a GetUsers method on it it will satisfy the interface so you will be able to use it on the App.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!