是否有任何选择使用类似于Mockito参数绑架者的东西



我们正在使用gopkg.in/mgo.v2/bson与mongo进行交谈,其API人群传递了结构而不是返回结果,例如:

func (p *Pipe) One(result interface{}) error {...

当我想模拟使用该代码的代码时,就会发生问题。我想嘲笑此执行,并以某种方式在"结果"中获得化为价值。目前测试的具有:

query.EXPECT().One(gomock.Any())

您可以看到,我没有任何值,我只是配置Gomock以检查我运行方法时,请查询一个。我不能像

那样通过结构
mystruct := MyStruct{}
query.EXPECT().One(&mystruct)

因为测试代码和实际代码中的mystruct是不同的,并且验证模拟会失败(引用是不同的(。我正在寻找类似于Mockito的论点绑架者的东西:https://static.javadoc.io/org.mockito/mockito-core/2.6.9/org/mockito/mockito/argumentcaptor.html

这可以通过做。

复制&github示例的糊状。

var capturedArgs []int
someMock.
  EXPECT().
  SomeMethod(gomock.Any()).
  Do(func(arg int){
    capturedArgs = append(capturedArgs, arg)
  })

参考:https://github.com/golang/mock/mock/pull/149

这个项目可以帮助您:https://github.com/bouk/monkey。您可以替换功能并使用Bool变量检查使用。

called := false    
monkey.Patch(package.One, func(result interface{}) error {
    if result == expected {
       called := true
       return nil
    }
    return errors.new("not expected")
})

不要忘记还原您的原始功能。

defer monkey.Unpatch(package.One)

最新更新