testing - 如何在 Grails 集成测试中发布两个内容不同的帖子

标签 testing grails controller integration

我正在测试一个 Controller ,我不能发布两个内容不同的帖子。 下面是一个示例,其中我使用一些数据(post1,带有 json1)向 cardController 执行了一个帖子。然后,我执行另一篇文章,使用不同的数据(post2 和 json2)。但是我无法成功发布第二篇文章,因为我已经看到(调试应用程序)请求中的 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()
    def post2 = cardController.post()
}

最佳答案

谢谢,我可以使用 reset()、removeAllParameters() 和 clearAttributes()。 下面是示例:

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

关于testing - 如何在 Grails 集成测试中发布两个内容不同的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5344213/

相关文章:

Play 中内存过度使用!框架方法调用

ruby-on-rails - Rails 4.2 集成测试 - 有什么方法可以重用测试代码吗?

android - 如何将 R.ids 链接到实际的按钮 android 测试

django - 在 CI 期间在 Django 测试数据库中启用 PSQL hstore 扩展

grails - 如何解决针对类的 'No such property. “”的严重错误500内部服务器错误

grails - 无法上传照片Grails

grails - 在 Grails 的 Quartz 作业中注入(inject)服务

ruby-on-rails - ActiveRecord::RecordNotFound -- 找不到没有 ID 的用户

grails - 将JoinTables呈现为Grails中的列表

javascript - 测试库错误 : mockConstructor(. ..):渲染没有返回任何内容