0

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

4

1 回答 1

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()字符串,然后你可以用它做任何你想做的事情。规范的超类是无关紧要的。

于 2018-09-26T16:09:54.737 回答