【发布时间】:2014-05-28 15:51:50
【问题描述】:
我正在尝试为 Grails 2.4.0 中的 RestfulController 编写一些集成测试,以 JSON 格式响应。 index()-方法是这样实现的:
class PersonController extends RestfulController<Person> {
...
def index(final Integer max) {
params.max = Math.min(max ?: 10, 100)
respond listAllResources(params), [includes: includeFields]
}
...
}
集成测试如下所示:
void testListAllPersons() {
def controller = new PersonController()
new Person(name: "Person", age: 22).save(flush:true)
new Person(name: "AnotherPerson", age: 31).save(flush:true)
controller.response.format = 'json'
controller.request.method = 'GET'
controller.index()
assertEquals '{{"name":"Person", "age": "22"},{"name":"AnotherPerson", "age": "31"}}', controller.response.json
}
我不明白的是 controller.response.json 只包含“AnotherPerson”而不是两个条目。 当我使用 run-app 启动服务器并使用 Rest-Client 对其进行测试时,我得到了两个条目。 有什么想法吗?
【问题讨论】:
-
回复应该是
JSONArray。您将其声明为无效的JSONObject。您能否也显示断言失败消息。
标签: json grails integration-testing