dow*_*eit 6 mocking go testify
下面的函数描述了如何使用 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)
}
Run Code Online (Sandbox Code Playgroud)
是否可以返回除 args.Int(),之外的任何内容args.Bool()?args.String()如果我需要退货怎么办int64,或者定制struct。有什么方法或者我错过了什么吗?
例如:
func (m *someMock) doStuff(p *sql.DB, id int) (res int64, err error)
Run Code Online (Sandbox Code Playgroud)
wij*_*ick 14
是的,可以通过使用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)
Run Code Online (Sandbox Code Playgroud)
所以,你的例子是:
// 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)
Run Code Online (Sandbox Code Playgroud)
另外,如果你的返回值是一个指针(例如指向结构体的指针),你应该在执行类型断言之前检查它是否为nil。
| 归档时间: |
|
| 查看次数: |
6796 次 |
| 最近记录: |