activeweb - ActiveWeb:测试 JSON 响应属性和值
问题描述
测试 JSON 响应的首选方法是什么,尤其是要存在的属性和值?什么是正确的控制器类型来扩展:IntegrationSpec
, ControllerSpec
, AppIntegrationSpec
? 我没有在相应的测试部分找到正确的响应。谢谢你。
解决方案
如果在这个示例项目中有一个很好的例子: 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()
字符串,然后你可以用它做任何你想做的事情。规范的超类是无关紧要的。
推荐阅读
- android - 将 LiveData 对象作为 ViewModel 的参数公开还是由成员函数调用返回更好?
- c# - Linq distinct 和 max:如何去除重复项
- json - 如何确认在 Coldfusion 中接收到 Webhook POST
- sql - 致命错误:未捕获错误:调用未定义函数 mysql_query() 布局不正确
- android - Kotlin - 未调用 google 的 Place Autocomplete API onClick 的清除按钮侦听器
- mysql - 未传递对象值
- c# - 为所有用户运行一次服务器功能
- r - 当变量的级别不同时在 R 中处理测试/训练数据
- excel - Excel 2016 中的 OData 提要/Power Query 未检索全部/复制某些记录
- r - 基于第二个df(R)的两个属性对数据框进行子集化