spock 模拟:捕获 arg 和存根 return 值 - 可以分开吗?

spock mocks: capture arg and stub return value - can be separate?

我有一个测试要求:

  1. 验证交互(调用方法,并使用正确的参数)
  2. 模拟需要 return 一些良性的 return 值,以免触发任何副作用。

附加的代码可以做到这一点,但我想知道是否有更易读的方法来做到这一点。特别是,我认为最好将模拟位 (#1) 与存根位 (#2) 分开。

有什么建议吗?

谢谢!

def "foo"() {
    setup:
    Payload payload
    Collaborator mock = Mock()
    underTest.collaborator = mock

    when: "doing something"
    underTest.doSomething()

    then: "collaborator's func is called once"
    1 * mock.func(*_) >>  { args ->
        payload = args[0] // 1. capture arg for inspection
        SOME_RETURN_VAL // 2. return a canned response
    }
    and: "collaborator is passed correct args"
    with(payload) {
        //...do some verification over payload
    }
}

让我们咨询 Spock documentation,好吗?

Combining Mocking and Stubbing

Mocking and stubbing go hand-in-hand:

1 * subscriber.receive("message1") >> "ok"
1 * subscriber.receive("message2") >> "fail"

When mocking and stubbing the same method call, they have to happen in the same interaction. In particular, the following Mockito-style splitting of stubbing and mocking into two separate statements will not work:

given:
subscriber.receive("message1") >> "ok"

when:
publisher.send("message1")

then:
1 * subscriber.receive("message1")

As explained in "Where to Declare Interactions", the receive call will first get matched against the interaction in the then: block. Since that interaction doesn’t specify a response, the default value for the method’s return type (null in this case) will be returned. (This is just another facet of Spock’s lenient approach to mocking.). Hence, the interaction in the given: block will never get a chance to match.

NOTE

Mocking and stubbing of the same method call has to happen in the same interaction.