我写了以下规范:
class Spec extends Specification {
def 'does not work if interaction declared as Set without parens'() {
given:
def holder = Mock(Holder)
def expected = [1, 2, 3, 3]
when:
def output = holder.value()
then:
output == [1, 2, 3] as Set
// 1 * holder.value() >> expected.toSet() // 1
1 * holder.value() >> expected as Set // 2
// 1 * holder.value() >> (expected as Set) // 3
}
class Holder {
def value() {
}
}
}
问题在于,仅当holder.value()
交互定义为行 1 或 3 时,它才有效。当它在 2 行中定义时,它会失败并出现以下错误:
Condition not satisfied:
output == [1, 2, 3] as Set
| |
null false
为什么呢?似乎可能存在一些解析器错误。
答案 0 :(得分:4)
没有解析器错误,但您编写了一个编程错误。根据{{3}} as
的规则,优先级高于((1 * holder.value()) >> expected) as Set
,这有效地使您的错误代码等同于
nTh
Spock实现了一个用于测试的DSL,但代码仍然是Groovy。 ; - )