为了账号安全,请及时绑定邮箱和手机立即绑定

通过拉伸器/作证模拟,不同的返回参数

通过拉伸器/作证模拟,不同的返回参数

Go
繁花如伊 2022-05-23 16:51:56
下面的函数描述了如何使用 testify 进行模拟。args.Bool(0),args.Error(1)是模拟的位置返回值。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,或者定制struct。有什么方法还是我错过了什么?例如:func (m *someMock) doStuff(p *sql.DB, id int) (res int64, err error)
查看完整描述

1 回答

?
12345678_0001

TA贡献1802条经验 获得超5个赞

是的,可以通过使用args.Get和类型断言来实现。


从文档:


// 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)

所以,你的例子是:


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

    args := m.Called(p, id)

    return args.Get(0).(int64), args.Error(1)

}

另外,如果您的返回值是一个指针(例如指向结构的指针),您应该在执行类型断言之前检查它是否为 nil。


查看完整回答
反对 回复 2022-05-23
  • 1 回答
  • 0 关注
  • 121 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号