Search code examples
groovyspock

How do I specify a Range instead of List in the 'where:' block of a Spock specification


The following example code:

class MySpec extends spock.lang.Specification {
    def "My test"(int b) {
        given:
        def a = 1

        expect:
        b > a

        where:
        b << 2..4
    }
}

throws the following compilation error: "where-blocks may only contain parameterizations (e.g. 'salary << [1000, 5000, 9000]; salaryk = salary / 1000')"

but using a List instead of a Range:

        where:
        b << [2,3,4]

compiles and runs fine as expected.

Could I also specify a Range somehow?


Solution

  • Use

    where:
    b << (2..4)
    

    The test can be optimized as below as well. Note no arguments to the test.

    def "My test"() {
      expect:
      b > a
    
      where:
      a = 1
      b << (2..4)
    }