开发者

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

开发者 https://www.devze.com 2023-02-25 06:03 出处:网络
Object A { def a = { something} } // I\'ve import A, but still have er开发者_如何学编程ror message:not found: type A
Object A {
  def a = { something}
}

// I've import A, but still have er开发者_如何学编程ror message:  not found: type A
val x = mock[A]


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消