【问题标题】:ActiveWeb: testing JSON response attributes and valuesActiveWeb:测试 JSON 响应属性和值
【发布时间】:2018-09-26 15:22:47
【问题描述】:

测试 JSON 响应的首选方法是什么,尤其是要存在的属性和值?从IntegrationSpec、ControllerSpec、AppIntegrationSpec 扩展控制器的正确类型是什么?我没有在相应的Testing section 中找到正确的响应。谢谢。

【问题讨论】:

    标签: activeweb


    【解决方案1】:

    在这个示例项目中有一个很好的例子: https://github.com/javalite/activeweb-rest/blob/27687e26f5476734b867d01f38b2575c6de3f3c9/src/test/java/app/controllers/PeopleControllerSpec.java#L57

    public void shouldCreateNewPeopleWithAddresses(){
    
            String json = Util.readResource("/people.json");
            request().content(json.getBytes()).contentType("application/json").post("create"); // <--- need to call "create" according to REST routing
    
            Map response = JsonHelper.toMap(responseContent());
    
            the(response.get("code")).shouldBeEqual(200);
            the(response.get("message")).shouldBeEqual("successfully created people and addresses");
    }
    

    基本上,你得到一个responseContent() 作为字符串,然后你可以用它做任何你想做的事情。规范的超类是无关紧要的。

    【讨论】:

    • 感谢您的回复。 ...the super class for the spec is irrelevant 下的意思是什么?
    • 表示任何超类都可以,IntegrationSpec、ControllerSpec、AppIntegrationSpec
    • 还有一个愿望是杀死所有的专精超类,只留下一个,这可以做到。在这个方向上已经做了一些工作,但还没有完成。不过,它将向后兼容当前的超类。
    猜你喜欢
    • 2019-02-25
    • 1970-01-01
    • 1970-01-01
    • 2023-03-15
    • 2019-02-23
    • 1970-01-01
    • 1970-01-01
    • 2019-01-07
    • 1970-01-01
    相关资源
    最近更新 更多