Is there any way to do mock argument capturing in Spock

微笑、不失礼 提交于 2019-12-09 05:01:05

问题


I have looked around and tried different things to no avail. The examples out there on the interwebs are few, and IMHO pretty simple. My use case:

(the 'itocNetworkHandler' below is the mock)

when: "we're doing stuff"
    StandardResponse response = cms.doCardStuff("123", "111", order)
....
then: "we get proper calls and response object"
    1 * cms.itocNetworkHandler.doNetworkCall(
            { it instanceof ReplacementRequestRecord
            }, StandardResponseRecord.class) >> record

I would like to store away the parameter ('it') to the "doNetworkCall" on the mock.

The reason i want the parameter is because the object i'm testing is supposed to take my in parameters, do stuff, create a new object and pass that one to my mock. I want to make sure that the created object looks the way its supposed to.

Pointers much appreciated.


回答1:


You can capture an argument as follows:

// must be declared before when-block (or inside Specification.interaction {})
def captured

when:
...

then:
1 * mock.doNetworkCall(...) >> { record, recordClass -> 
    // save the argument
    captured = record
    ...
}
// use the saved argument
captured == ...

That said, often there is a simpler solution such as checking the expected record right in the argument constraint (e.g. ...doNetworkCall( { it == ... } )).



来源:https://stackoverflow.com/questions/16331230/is-there-any-way-to-do-mock-argument-capturing-in-spock

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!