如何使GrailsIntegrationTesting中有两个不同内容的帖子 [英] How to make two posts with different contents in GrailsIntegrationTesting
问题描述
我正在测试一个控制器,我不能做两个不同内容的帖子。
按照一个例子,其中我执行一个post到cardController,有一些数据(post1,与json1)。然后,我执行另一个帖子,与不同的数据(post2与json2)。但我不能使第二个post成功,因为我看到(调试应用程序),请求中的json,是json1,而不是josn2。
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 b $ b def post2 = cardController.post()
}
下面是示例:
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()
}
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()
}
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()
}
这篇关于如何使GrailsIntegrationTesting中有两个不同内容的帖子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!