繁体   English   中英

如何在Grails Integration测试中使用不同的数据创建多个请求

[英]How to make multiple requests with different data in Grails Integration tests

我正在使用Spock插件编写Grails 2.2.1集成测试,其中我试图将两组数据发布到同一个控制器端点:

when: "The user adds this product to the inventory"
def postData = [productId: 123]
controller.request.JSON = postData
controller.addToInventory()

and: "Then they add another"
def secondPostData = [productId: 456]
controller.request.JSON = secondPostData
controller.addToInventory()

then: "The size of the inventory should be 2"
new JSONObject( controller.response.contentAsString ).inventorySize == 2

我看到的问题是,对于两个请求,都将相同的JSON提交给addToInventory()。

这个StackOverflow问题建议调用controller.request.reset(),但这不起作用(没有方法签名:org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest.reset())。

我正在尝试的是什么?

“where:”可用于在spock测试框架中执行数据驱动测试。 尝试使用以下示例:

when: "The user adds this product to the inventory"

controller.params.JSON = [productId:productId]
controller.addToInventory()

then: "The size of the inventory should be 2"
new JSONObject( controller.response.contentAsString ).inventorySize == 2

where:

ID|productId
1|123
2|456

希望有所帮助!

实际上还有另一种方式。 插入:

GrailsWebUtil.bindMockWebRequest()

在第二次测试开始时。 这将有效地创建一个新的ServletContext,一个新的MockHttpServletRequest,一个新的MockHttpServletResponse,然后将所有三个绑定到当前线程中。

虽然Anuj我们纠正了应该使用where子句来保持测试干净,但有时候测试需要运行多个请求。 在我的情况下,我想测试当它收到2个重复的POSTS,第二个被正确拒绝。

我发现重置响应做了我需要的:

controller.response.reset()

清除回应。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM