3
我试图验证以下方法被使用的Mockito称为:匹配任何参数的功能Scala的一个参数的Mockito
class Notifier {
def forward(request: ServletRequest)(onFailure: => Unit) : Unit
}
这里有一个模拟验证:
val notifier = mock[Notifier]
there was one(notifier).forward(any[ServletRequest])(any[() => Unit])
我得到的例外:
The mock was not called as expected:
Invalid use of argument matchers!
3 matchers expected, 2 recorded.
This exception may occur if matchers are combined with raw values:
//incorrect:
someMethod(anyObject(), "raw String");
When using matchers, all arguments have to be provided by matchers.
For example:
//correct:
someMethod(anyObject(), eq("String by matcher"));
我知道这是由最后一个无参数函数引起的。我如何在这里正确执行验证?
'=> Unit'不是一个函数类型,而是一个名称参数。 –
@ BenJames那么'(onFailure:Unit)'与'(onFailure:=> Unit)'有什么不同? –
区别在于代码块执行时。有一个参数向前(onFailure:Unit)没有意义。在调用forward之前,将会评估参数。使用forward(onFailure:=> Unit)方法可以决定何时评估参数。 –