mike rodent
mike rodent

Reputation: 15632

How to make a mocked method return a succession of values with a succession of invocations

This is such a basic question I'm sure I'll have to delete as a dupe... but I haven't found the answer.

In the test: I thought I'd try something like this. What I'm trying to do is get it to return '333' the first call and '555' the second call.

gb.reader = Mock( XMLStreamReader ){
    getAttributeValue( _, _ ) >> [ '333', '555' ]
}

In the app code:

int id = reader.getAttributeValue( null, 'ID').toInteger()

The result:

java.lang.NumberFormatException: For input string: "[333, 555]"

First thought that comes to mind: we need a generator. But this is Groovy, not Python.

Upvotes: 0

Views: 61

Answers (1)

kriegaex
kriegaex

Reputation: 67297

Just use >>>, see the Spock manual, chapter "returning sequences of values".

Upvotes: 1

Related Questions