How do I mock static function (Object function, not class function) in scala

ぐ巨炮叔叔 提交于 2019-12-06 05:19:28

问题


Object A {
  def a = { something}
}

// I've import A, but still have error message:  not found: type A
val x = mock[A]

回答1:


You may find this email thread instructive.

Whilst pure mocking of the object is not possible with any tool yet, the thread above does have a few options for you. All of which involve changing your design to some degree.




回答2:


You don't. Not only A is not a type or class -- it is an instance -- but it is an instance of a singleton (A.type).

What you do instead is put your methods on a trait, and make the object extend it. Then, you mock the trait instead of mocking the object.



来源:https://stackoverflow.com/questions/5696815/how-do-i-mock-static-function-object-function-not-class-function-in-scala

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!