I tried to return a list of records as JSON from the controller as follows:
public class ListsController extends APIController {
public void index(){
respond(List.findAll().toJson(true)).contentType("application/json");
}
}
ApiController is defines as follows:
public abstract class APIController extends AppController {
@Override
protected String getContentType() {
return "application/json";
}
@Override
protected String getLayout() {
return null;
}
}
When I test it:
@Test
public void shouldRenderLists(){
app.models.List list = new app.models.List();
list.set("id", 1);
list.set("code", "some code");
list.set("name", "some name");
list.set("item_code", "some item code");
list.saveIt();
request().integrateViews().get("index");
the(contentType()).shouldBeEqual("application/json");
Map[] listsMaps = JsonHelper.toMaps(responseContent());
the(listsMaps.length).shouldBeEqual(1);
Map listEntry = listsMaps[0];
the(listEntry.get("code")).shouldBeEqual("some code");
the(listEntry.get("name")).shouldBeEqual("some name");
}
It fails because responseContent()
returns an [\n\n
]. Any ideas why so ?