如果使用 'as' 定义,Spock 交互将不起作用
Spock interaction does not work if defined with 'as'
我写了以下规范:
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
为什么?似乎可能存在一些解析器错误。
没有解析器错误,但您出现了编程错误。根据 Groovy operator precedence 的规则 >>
比 as
具有更高的优先级,这实际上使您的错误代码等同于
((1 * holder.value()) >> expected) as Set
Spock 实现了用于测试的 DSL,但代码仍然是 Groovy。 ;-)
我写了以下规范:
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
为什么?似乎可能存在一些解析器错误。
没有解析器错误,但您出现了编程错误。根据 Groovy operator precedence 的规则 >>
比 as
具有更高的优先级,这实际上使您的错误代码等同于
((1 * holder.value()) >> expected) as Set
Spock 实现了用于测试的 DSL,但代码仍然是 Groovy。 ;-)