Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

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

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

Is what I am attempting possible?

like image 548
Bryan O'Sullivan Avatar asked Sep 12 '13 11:09

Bryan O'Sullivan


1 Answers

"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

Hope that helps!!!

like image 89
Anuj Aneja Avatar answered Sep 27 '22 21:09

Anuj Aneja