【发布时间】:2020-06-06 01:34:11
【问题描述】:
我在 Spock 交互文档中发现了一条有趣的语句:
http://spockframework.org/spock/docs/1.3/interaction_based_testing.html#_argument_constraints
最后一行约束,带有闭包谓词的例子:
1 * subscriber.receive({ it.size() > 3 && it.contains('a') })
我的问题是:Groovy 中有没有办法将此谓词作为变量传递?
我的测试环境代码:
class Something {
Doer doer
Something(Doer doer) {
this.doer = doer
}
boolean doSth(x) {
if (!doer.validate(x)) throw new RuntimeException()
true
}
}
class Doer {
boolean validate(int x) {
x == 2
}
}
和测试代码:
def "some test"() {
given:
def d = Mock(Doer)
def s = new Something(d)
when:
s.doSth(2)
then:
1 * d.validate({ it == 2 }) >> true
}
我想要达到的目标:
def "some test"() {
given:
def d = Mock(Doer)
def s = new Something(d)
def myClosureVar = { ??? }
when:
s.doSth(2)
then:
1 * d.validate(myClosureVar) >> true
}
【问题讨论】:
-
我不相信有,我似乎记得它需要在测试的实际定义中,这样 spock 才能使用它的 DSL 魔法