如何在Grails集成测试中使用不同的数据发出多个请求 [英] How to make multiple requests with different data in Grails Integration tests

查看:41
本文介绍了如何在Grails集成测试中使用不同的数据发出多个请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm writing a Grails 2.2.1 integration test using the Spock plugin, in which I am trying to post two sets of data to the same controller endpoint:

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().

The problem I am seeing is that the same JSON is submitted to addToInventory() for both requests.

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

This StackOverflow question suggests calling controller.request.reset(), but this did not work (No signature of method: org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest.reset()).

我正在尝试什么吗?

推荐答案

"Where:"可用于在Spock测试框架中执行数据驱动的测试.尝试使用以下示例:

"Where:" can be used to perform data driven testing in spock testing framework. Try, using the following example:

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

希望有帮助!

这篇关于如何在Grails集成测试中使用不同的数据发出多个请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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