尝试使用Spock模拟来验证方法调用参数。文档和互联网上的大多数示例都包含单个参数的示例。
如何在模拟方法调用中实现捕获和验证多个参数?
interface MyAction{
String doSomething(int first, String second);
}
// spec
def "should do something"(){
given:
MyAction action = Mock()
when:
action.doSomething(123, "abc")
then:
// how to verify that (first == 123) and (second == "abc") ?
}
就像对单个参数那样列出它们。
import spock.lang.*
class ASpec extends Specification {
// spec
def "should do something"(){
given:
MyAction action = Mock()
when:
action.doSomething(123, "abc")
then:
1 * action.doSomething(123, "abc")
}
}
interface MyAction{
String doSomething(int first, String second);
}
在Groovy Web控制台中试试
编辑:一个不匹配的例子将显示哪个参数不匹配以及为什么不匹配。
Too few invocations for:
1 * action.doSomething(123, "abc") (0 invocations)
Unmatched invocations (ordered by similarity):
1 * action.doSomething(123, 'bc')
One or more arguments(s) didn't match:
0: <matches>
1: argument == expected
| | |
bc | abc
false
1 difference (66% similarity)
(-)bc
(a)bc