gpt4 book ai didi

unit-testing - 第三方对象的 Grails/Groovy 模拟动态属性

转载 作者:行者123 更新时间:2023-12-02 15:08:28 24 4
gpt4 key购买 nike

def client = new SOAPClient("http://...")
def response = client.send(
"""<?xml version='1.0' encoding='UTF-8'?>
<soap-env:Envelope xmlns:SOAP='http://schemas.xmlsoap.org/soap/envelope/'>
<soap-env:Body>
<GetFoo>bar</GetFoo>
</soap-env:Body>
</soap-env:Envelope>"""
)
return response.xmlElement1.xmlElement2.xmlElement3.text()

这是我项目中的一段服务代码。 def response 是一个名为 HTTPResponse 的 WsLite 库对象。

在我的单元测试中,我想将此响应作为此代码的一部分进行模拟:
def mockClient = Mock(SOAPClient)
mockClient.send(XML_REQUEST) >> createMockResponse()

我不太确定我应该如何模拟这个响应对象并填充一些预期值。

例如,使用 response.xmlElement1.xmlElement2.xmlElement3.text() 模拟响应是'ABC'。

最佳答案

使用预期的返回值模式 stub 返回映射的响应

mockClient.send(XML_REQUEST) >> {
[ xmlElement1: [ xmlElement2: [ xmlElement3: [ text : { 'ABC' } ] ] ] ]
}

assert response.xmlElement1.xmlElement2.xmlElement3.text() == 'ABC'

关于unit-testing - 第三方对象的 Grails/Groovy 模拟动态属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21819280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com