How to make multiple queries with different data in Grails integration tests

I am writing a Grails 2.2.1 integration test using the Spock plugin in which I am trying to send two data sets 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 that I see is that the same JSON is sent to addToInventory () for both requests.

https://stackoverflow.com/questions/692256/... suggests invoking controller.request.reset (), but this did not work (There is no method signature: org.codehaus.groovy.grails.plugins.testing.GrailsMockHttpServletRequest.reset ()).

Am I trying to do this?

+8
testing grails integration spock
source share
3 answers

Where: can be used to perform data-driven testing as part of testing testing. 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 this helps !!!

+6
source share

There is another way. Insert:

 GrailsWebUtil.bindMockWebRequest() 

at the beginning of your second test. This will effectively create a new ServletContext, a new MockHttpServletRequest, a new MockHttpServletResponse, and then bind all three to the current thread.

+2
source share

While Anuj us correct that where clauses should be used to keep the tests clean, there are times when more than one query needs to be run in a test. In my case, I wanted to check that when he received 2 repeated POSTS, the second was rejected correctly.

I found that resetting the response did what I needed:

 controller.response.reset() 

To cancel the response.

0
source share

All Articles