Mock out method base return by the number of calls only in spock

Is it possible to ridicule the return value of a method in spock based on the nth time of its call? Please note: I do not want to specify the passed parameters, because it does not matter for a specific test case.

For example, for the first call, it must return x, for the second call, it must return y.

+4
source share
1 answer

Yes it is possible.

someObject.someMethod(*_) >>> [ 'x', 'y' ]

It will return xon the first call and yon the second method call.

Example:

void "test something"() {
    given:
    def sample = Mock(Sample){
        someMethod(_) >>> [ 'Hello', 'World' ]
    }

    expect:
    sample.someMethod( 'foo' ) == 'Hello'
    sample.someMethod( 'bar' ) == 'World'
}

class Sample {
    def someMethod(def a) {
        return a
    }
}
+6
source

All Articles