Spock 单元测试,groovy 左移赋值抛出 SpockExecutionException:数据提供者没有数据

Spock unit test, groovy leftshift assignment is throwing a SpockExecutionException: Data provider has no data

我正在编写一个 spock 单元测试,当我尝试使用 groovy collect

动态提供数据提供程序时,出现以下错误
SpockExecutionException: Data provider has no data

这是我可以提供的最简单的引发错误的情况:

import spock.lang.Shared
import spock.lang.Specification

class SampleTest extends Specification {

    @Shared
    def someArray

    void setup() {
        someArray = ['a','b','c']
    }

    def "ensure that 'Data provider has no data' is not thrown"() {

        expect:
        columnA == columnB

        where:
        [columnA, columnB] << someArray.collect { value -> [value, value] }
    }
}

groovy 代码似乎有效。这是我在 groovy 控制台上的测试:

def someArray = ['a','b','c']
def test = someArray.collect { value -> [value, value] }
println test

[[a, a], [b, b], [c, c]]

我误会了什么?

我正在使用:

使用 setupSpec() 而不是 setup() 以您想要的方式访问 @Shared 变量,如 @Shared documentation 中所示。或者,您也可以在声明期间初始化共享变量。

import spock.lang.*

class SampleTest extends Specification {

    @Shared someArray

    // This is similar to just using
    // @Shared someArray = ['a','b','c']
    // Use above instead of setupSpec() if required
    // setupSpec() is invoked before any test case is invoked
    void setupSpec() {
        someArray = ['a','b','c']
    }

    def "ensure that 'Data provider has no data' is not thrown"() {
        expect:
        columnA == columnB

        where:
        [columnA, columnB] << someArray.collect { [it, it] }
    }
}