【问题标题】:Spock - mock a method different arguments and different return valuesSpock - 模拟方法不同的参数和不同的返回值
【发布时间】:2019-02-01 12:08:28
【问题描述】:
given:
def someService = Mock(SomeService)

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

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

2 * someService.processInput(argument1) >>> [output1, output2]

【问题讨论】:

  • 个人意见:测试应该是可读的。你为什么要这样做?从技术上讲,德米特里的回答可以满足您的需求,但如果我是您,我不会那样做(当然我不是)。

标签: groovy spock


【解决方案1】:

我相信目前在 Spock 中不可能以您可能期望的优雅方式。我只是想出了以下内容:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-02
    • 1970-01-01
    • 1970-01-01
    • 2022-01-04
    • 2016-01-30
    相关资源
    最近更新 更多