开发者

mock a function with a callback function as parameter

开发者 https://www.devze.com 2023-02-26 02:29 出处:网络
My code structure is l开发者_StackOverflowike below: class A { def a(x: () => Unit) { do something}

My code structure is l开发者_StackOverflowike below:

class A {
  def a(x: () => Unit) { do something}
}

class B {
  .... 
  def foo() {
    def x() { something }
    a(x)
  }
}

Now I want to do unittest of class B with a mock A.

val a = mock[A]
def x () { ... }
a.a(x) atLeastOnce

The above doesn't work. Since this new x is not the x inside foo(). But the x inside foo is a local one, not accessible to unittest. Any suggestion except to move x out of foo?


You have to mock out the function literal passed into A.a. Please look into the answer of the following SOF question and see whether that helps

How to mock a method with functional arguments in Scala?

0

精彩评论

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