我正在编写一个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]]
我误解了什么?
我正在使用:
答案 0 :(得分:6)
使用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] }
}
}