duancheng7743
2018-02-23 10:53
浏览 200
已采纳

gomock,Go,芒果包,MongoMock

I am trying to mock the below method using gomock

func GetS(tenantName string) (*mgo.Session, error) {
    ctx := apiContext.TContext{}
    url, err := connectionURLList.get(tenantName)
    if err != nil {
        log.GenericWarning(ctx,
            fmt.Sprintf("connection to %s not yet created, creating one: %v", tenantName, err), nil)
        if err := connectMongo(tenantName); err == nil {
            return GetS(tenantName) //singleton recursion to again call GetS
        }
        return nil, err
    }
    // ignoring error, expected we will always setting session in session map
    session, _ := connectionList.get(url)
    return session.Copy(), err
}

My Interface

type MongoManager interface {
    GetS(tenantName string) 
}

func TestGetS(t *testing.T) {
    //var mgoCall *mgo.Session
    mockCtrl := gomock.NewController(t)
    defer mockCtrl.Finish()
    mockManagerObj := mocks.NewMockMongoManager(mockCtrl)
    mockManagerObj.EXPECT().GetS("cacargroup").Return(nil)
}

I am Getting the below error . Can someone help

 $ go test

--- FAIL: TestGetS (0.00s) mongoManager_test.go:20: missing call(s) to *mocks.MockMongoManager.GetS(is equal to cacargroup) /Users/charles/workspace/src/bitbucket.org/tekion/tbaas/mongoManager/mongoManager_test.go:16 mongoManager_test.go:20: aborting test due to missing call(s) FAIL exit status 1

图片转代码服务由CSDN问答提供 功能建议

我正在尝试使用吊床模拟以下方法

   func GetS(tenantName字符串)(* mgo.Session,错误){
 ctx:= apiContext.TContext {} 
 url,err:= connectionURLList.get(tenantName)
如果err!= nil {
日志。  GenericWarning(ctx,
 fmt.Sprintf(“尚未建立与%s的连接,创建一个:%v”,tenantName,err),nil)
如果err:= connectMongo(tenantName);  err == nil {
 return GetS(tenantName)//单递归再次调用GetS 
} 
 return nil,err 
} 
 //忽略错误,期望我们将始终在会话映射中设置会话
 会话,_:= connectionList.get(url)
返回会话。Copy(),err 
} 
   
 
 

我的界面 \ n

  type MongoManager接口{
 GetS(tenantName字符串)
} 
 
func TestGetS(t * testing.T){
 // var mgoCall * mgo.Session 
 mockCtrl:= gomock  .NewController(t)
延迟嘲笑嘲笑嘲讽。 代码>  
 
 

我正在收到以下错误消息。 有人可以帮助

  $去测试
   
 
 

---失败:TestGetS(0.00 s) mongoManager_test.go:20:缺少对* mocks.MockMongoManager.GetS的调用(等于cacargroup)/Users/charles/workspace/src/bitbucket.org/tekion/tbaas/mongoManager/mongoManager_test.go :16 mongoManager_test.go:20:由于缺少呼叫而导致测试失败FAIL退出状态1

  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • doumiang0597 2018-02-25 19:31
    已采纳

    You see actually the method in your interface implemented with return type of an error. But you are using like it returns nothing and chaining the implementation. Just remove the return type of GetS.

    type fn func(string) (*mgo.Session, error)
    
    type MongoManager interface {
        NewFunction(GetS, "cascade")
    }
    
    func TestGetS(t *testing.T) {
        //var mgoCall *mgo.Session
        mockCtrl := gomock.NewController(t)
        defer mockCtrl.Finish()
        mockManagerObj := mocks.NewMockMongoManager(mockCtrl)
        mockManagerObj.EXPECT().GetS("cacargroup").Return(nil)
    }
    

    Also you have to remove it from GetS function too

    func NewFunction(GetS fn, value string){
       GetS("cascade")
    }
    
    func GetS(tenantName string) (*mgo.Session, error){
        ctx := apiContext.TContext{}
        url, err := connectionURLList.get(tenantName)
        if err != nil {
            log.GenericWarning(ctx,
                fmt.Sprintf("connection to %s not yet created, creating one: %v", tenantName, err), nil)
            if err := connectMongo(tenantName); err == nil {
                return GetS(tenantName) //singleton recursion to again call GetS
            }
            return nil, err
        }
        // ignoring error, expected we will always setting session in session map
        session, _ := connectionList.get(url)
    }
    
    打赏 评论

相关推荐 更多相似问题