有什么方法可以在Spock中进行模拟参数捕获 [英] Is there any way to do mock argument capturing in Spock

查看:328
本文介绍了有什么方法可以在Spock中进行模拟参数捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我环顾四周,尝试了不同的尝试,但都无济于事.互联网上的示例很少,恕我直言非常简单.我的用例:

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:

(下面的"itocNetworkHandler"是模拟对象)

(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

我想将参数('it')存储到模拟中的"doNetworkCall".

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

我想要参数的原因是因为我要测试的对象应该采用我的in参数,执行操作,创建一个新对象并将该对象传递给我的模拟对象.我想确保创建的对象看起来像它应该的样子.

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.

非常感谢指针.

推荐答案

您可以按以下方式捕获参数:

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 == ...

也就是说,通常有一个更简单的解决方案,例如检查参数约束中的预期记录(例如...doNetworkCall( { it == ... } )).

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

这篇关于有什么方法可以在Spock中进行模拟参数捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆