How to make two posts with different contents in GrailsIntegrationTesting

戏子无情 提交于 2020-01-02 09:28:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!