Go 通过stretchr/confidence进行模拟,不同的返回参数

Go 通过stretchr/confidence进行模拟,不同的返回参数,go,mocking,testify,Go,Mocking,Testify,下面的函数描述了如何使用“验证”进行模拟。args.Bool0、args.Error1是模拟位置返回值 func (m *MyMockedObject) DoSomething(number int) (bool, error) { args := m.Called(number) return args.Bool(0), args.Error(1) } 是否可以返回args.Int、args.Bool、args.String以外的任何内容?如果我需要返回int64或自定义结构,该

下面的函数描述了如何使用“验证”进行模拟。args.Bool0、args.Error1是模拟位置返回值

func (m *MyMockedObject) DoSomething(number int) (bool, error) {

  args := m.Called(number)
  return args.Bool(0), args.Error(1)

}
是否可以返回args.Int、args.Bool、args.String以外的任何内容?如果我需要返回int64或自定义结构,该怎么办。有没有办法,或者我遗漏了什么

例如:

func (m *someMock) doStuff(p *sql.DB, id int) (res int64, err error)

是的,可以使用args.Get和类型断言

从:

因此,你的例子是:

func m*someMock doStuffp*sql.DB,id int res int64,err error{ args:=m.Calledp,id 返回args.Get0.int64,args.Error1 }
另外,如果返回值是指针(例如指向结构的指针),则在执行类型断言之前,应检查它是否为nil。

是的,可以使用args.Get和类型断言

从:

因此,你的例子是:

func m*someMock doStuffp*sql.DB,id int res int64,err error{ args:=m.Calledp,id 返回args.Get0.int64,args.Error1 }
此外,如果返回值是指针(例如指向结构的指针),则在执行类型断言之前,应检查它是否为nil。

Go不支持在返回类型或参数上重载用户定义函数。但是,您可以使用两个名称不同的函数。这不是关于重载,而是关于验证libraryGo不支持在返回类型或参数上重载用户定义的函数。但是,您可以使用两个名称不同的函数。这与重载无关,而是与库有关
// For objects of your own type, use the generic Arguments.Get(index) method and make a type assertion:
//
//     return args.Get(0).(*MyObject), args.Get(1).(*AnotherObjectOfMine)