mock a function with a callback function as parameter
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?
精彩评论