我如何在实现中存根使用ClassTag
的方法?
class RefsFactory {
def get[I <: Item : ClassTag]: RefTo[I] = {
val itemType = implicitly[ClassTag[A]].runtimeClass.asInstanceOf[Class[A]]
// ...
}
}
该类在我们的代码中使用很多,我想对其进行存根处理以返回关于itemType
的其他模拟。
val factory = stub[RefsFactory]
val otherType = stub[RefTo[OtherType]]
(factory.get[OneType]) returns RefTo(new OneType())
(factory.get[OtherType]) returns otherType
感谢
试图简化您的问题,方法
def get[I <: Item : ClassTag]: RefTo[I]
类似于将上下文绑定到0-arity方法上
def foo[I: ClassTag]
等效于带有一个隐式参数的方法
def foo[I](implicit ev: ClassTag[I)
因此考虑Methods with implicit parameters,我们可以像这样嘲笑
(myMock.foo[SomeType](_: ClassTag[SomeType])).expects(*).returns(...)