问题
I'm testing a controller, and I cannot make two posts with different contents. Follows an example, in which I execute a post to the cardController, with some data (post1, with json1). Then, I execute another post, with different data (post2 with json2). But I cannot make the second post succesfully, because I've seen (debuggin the application), that the json in the request, is json1 again, and not josn2. So, how can I make two different posts in the same test?
void testSomething(){
def json1 = [number: "345678000000007", exp_month: 5, exp_year: 2012] as JSON
def strJson1 = json1 as String
cardController.request.contentType = "text/json"
cardController.request.content = strJson1.getBytes()
def post1 = cardController.post()
def json2 = [number: "345678000000009", exp_month: 5, exp_year: 2013] as JSON
def strJson2 = json2 as String
cardController.request.contentType = "text/json"
cardController.request.content = strJson2.getBytes()
def post2 = cardController.post()
}
回答1:
Thanks, I could with reset(), removeAllParameters(), and clearAttributes(). Below is the example:
void testSomething(){
def json1 = [number: "345678000000007", exp_month: 5, exp_year: 2012] as JSON
def strJson1 = json1 as String
cardController.request.contentType = "text/json"
cardController.request.content = strJson1.getBytes()
def post1 = cardController.post()
cardController.response.reset()
cardController.request.reset()
cardController.request.removeAllParameters()
cardController.request.clearAttributes()
def json2 = [number: "345678000000009", exp_month: 5, exp_year: 2013] as JSON
def strJson2 = json2 as String
cardController.request.contentType = "text/json"
cardController.request.content = strJson2.getBytes()
def post2 = cardController.post()
}
回答2:
Try calling cardController.response.reset()
after def post1 = cardController.post()
. It's not expected that you'll make two requests per test method, so you need to do some cleanup.
来源:https://stackoverflow.com/questions/5344213/how-to-make-two-posts-with-different-contents-in-grailsintegrationtesting