dongqiang4819 2015-01-29 07:43
浏览 35
已采纳

在Golang中测试第三者套餐

I'm new to golang and trying to write a simple learning app using the facebook package from https://github.com/huandu/facebook.

I was able to get the package and connect to facebook and hit the facebook API. This is great but testing is my concern.

At first I was simply calling the method and creating a facebook object inside of it. Then after some research I tried passing in the facebook method I wanted to mock. Realizing there were multiple methods I needed, I am convinced that passing an interface is the right way to go.

So I tried creating interfaces that the package would implement.

type IFbApp interface {
    ExchangeToken(string) (string, int, error)
    Session(string) IFbSession
}

type MyFbApp struct{}

func (myFbApp *MyFbApp) ExchangeToken(token string) (string, int, error) {
    return myFbApp.ExchangeToken(token)
}

func (myFbApp *MyFbApp) Session(token string) IFbSession {
    return myFbApp.Session(token)
}

type IFbSession interface {
    User() (string, error)
    Get(string, map[string]interface{}) (map[string]interface{}, error)
}

type MyFbSession struct{}
func (myFbSession *MyFbSession) User() (string, error) {
    return myFbSession.User()
}

func (myFbSession *MyFbSession) Get(path string, params map[string]string) (map[string]string, error) {
    return myFbSession.Get(path, params)
}

func SomeMethod() {
    Facebook(fb.New("appId", "appSecret")); // fb calls package
}

func Facebook(fbI IFbApp) {
    fbI.ExchangeToken("sometokenhere");
}

I cannot compile this code since I get an error

cannot use facebook.New("appId", "appSecret") (type *facebook.App) as type IFbApp in argument to Facebook:
    *facebook.App does not implement IFbApp (wrong type for Session method)
        have Session(string) *facebook.Session
        want Session(string) IFbSession

Switching IFbSession to *facebook.Session would make it compile of course but then I need to mock methods from the Session struct as well.

My plan was to create mock structs that implement my interfaces in my test.go file and pass it to the methods under test. Is this the right way to go?

I'd like to stay pure golang as much as possible and stay away from mocking frameworks.

Thanks.

  • 写回答

1条回答 默认 最新

  • doute7910 2015-01-30 05:35
    关注

    You can implement a wrapper for fb.App and override Session method to return IFbSession instead of Facebook.Session.

    package main
    
    import fb "github.com/huandu/facebook"
    
    type IFbApp interface {
        ExchangeToken(string) (string, int, error)
        Session(string) IFbSession
    }
    
    type MockFbApp struct {
        IFbApp
    }
    
    func (myFbApp *MockFbApp) ExchangeToken(token string) (string, int, error) {
        return "exchangetoken", 1, nil
    }
    
    func (myFbApp *MockFbApp) Session(token string) IFbSession {
        return &MyFbSession{}
    }
    
    type IFbSession interface {
        User() (string, error)
        Get(string, fb.Params) (fb.Result, error)
    }
    
    type MyFbSession struct {
        IFbSession
    }
    
    func (myFbSession *MyFbSession) User() (string, error) {
        return "userid", nil
    }
    
    func (myFbSession *MyFbSession) Get(path string, params fb.Params) (fb.Result, error) {
        return fb.Result{}, nil
    }
    
    type RealApp struct {
        *fb.App
    }
    
    func (myFbApp *RealApp) Session(token string) IFbSession {
        return myFbApp.App.Session(token)
    }
    
    func SomeMethod() {
        Facebook(&MockFbApp{})
        Facebook(&RealApp{fb.New("appId", "appSecret")})
    }
    
    func Facebook(fbI IFbApp) {
        fbI.ExchangeToken("sometokenhere")
        fbI.Session("session")
    }
    
    func main() {
        SomeMethod()
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?