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


given:
def someService = Mock(SomeService)
1 * someService.processInput(argument1) >> output1
1 * someservice.processInput(argument2) >> output2

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

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
    }
}

最新更新