Question

I'm using the Spock framework for testing, and I have a question regarding multivaraible assignment in a where clause.

I have the following test case:

def "sending a message delegates to message sender"() {
  when:
    sendMessage(x,y)
  then:
    1 * messageSender.send(x,y)
  where:
    x << 1
    y << 2
}

I want to replace the multiple variable assignments in the where clause with a single assignment operation. I tried:

where:
  [x,y] << [1,2]

but got a MissingMethod exception. I assume this is because the expression [1,2] is treated as an array rather than a list.

Note that the following worked:

where:
  [x,y] << [1,2].combinations()

It seems that the combinations() method returns a List type, but despite that the following did not work:

where:
  [x,y] << [1,2].asList()

Using combinations() is counter-intuitive, so I am wondering if there is a simple, elegant way of initializing multiple variables in Spock.

edit: I am using spock version 0.7-groovy-2.0

Was it helpful?

Solution

Multiple assignment has to be done as mentioned below:

[x, y] << [[1,2]]

Here is a contrived example where test fails for one combination.

Multiple variable data pipes uses an example from SQL ResultSet which is pretty much similar to the above example.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top