Groovy Spock-模拟不同参数和不同返回值的方法

Groovy Spock-模拟不同参数和不同返回值的方法,groovy,spock,Groovy,Spock,如何在一个语句中使用和带有不同参数的子句。例如: given: def someService = Mock(SomeService) 1 * someService.processInput(argument1) >> output1 1 * someservice.processInput(argument2) >> output2 我认为,目前在斯波克,你可能期望的优雅方式是不可能的。我只想到了以下几点: 2 * someService.processInput

如何在一个语句中使用
和带有不同参数的
子句。例如:

given:
def someService = Mock(SomeService)

1 * someService.processInput(argument1) >> output1
1 * someservice.processInput(argument2) >> output2

我认为,目前在斯波克,你可能期望的优雅方式是不可能的。我只想到了以下几点:

2 * someService.processInput(argument1) >>> [output1, output2]
不确定它是否符合您的期望。下面是测试此方法的完整规范

def args = [arg1, arg2]
2 * service.processInput({ it == args.removeAt(0) }) >>> [out1, out2]

个人观点:测试应该是可读的。你为什么要这么做?德米特里的回答在技术上符合你的要求,但如果我是你,我不会那样做(当然我不是)。
class SOSpec extends Specification {
    def "mock a method different arguments and different return values"() {
        when:
        def arg1 = "F"
        def arg2 = "B"
        def out1 = "Foo"
        def out2 = "Bar"
        def service = Mock(SomeService) {
            def args = [arg1, arg2]
            2 * processInput({ it == args.removeAt(0) }) >>> [out1, out2]
        }

        then:
        service.processInput(arg1) == out1
        service.processInput(arg2) == out2
    }
}