Monday, January 3, 2011

Another undocumented annoyance with testing Grails controllers

I switched from rendering JSON in my previous post to using a gsp to format an HTML ul tag based on a model containing my list of results. This is because I find it easier when using JQuery to do my AJAX using HTML content. JQuery seems to natively prefer HTML to JSON, whereas Dojo seemd to run on JSON.

At any rate, I changed my controller to something like this:

def loadTree = {
def parent = params['dir'] "loading tree for parent path: ${parent}"
def collectionAndDataObjectListAndSearchAO = irodsAccessObjectFactory.getCollectionAndDataObjectListAndSearchAO(irodsAccount)
def collectionAndDataObjectList = collectionAndDataObjectListAndSearchAO.listDataObjectsAndCollectionsUnderPath(parent)
log.debug("retrieved collectionAndDataObjectList: ${collectionAndDataObjectList}")
render(view:"loadTree",model:[collectionAndDataObjectList:collectionAndDataObjectList, parent:parent])

I altered my unit test, per the docs, to inspect the model and view. I attempted to get at the model entry for 'parent', which contained the parent dir, like this:

def parent = mav.model.parent

It turns out that that does not work anymore, instead, you need to interpose a reference to linkedHasMap like so:

void testBrowse() {
controller.params.dir = "/"
controller.irodsAccessObjectFactory = irodsAccessObjectFactory
controller.irodsAccount = irodsAccount
def mav = controller.modelAndView
def name = mav.viewName
assertNotNull("null mav", mav)
assertEquals("view name should be loadTree", "loadTree", name)
def parent = mav.model.linkedHashMap.parent
assertEquals("parent dir not found", "/", parent)


Now my Grails controller and test are working again. My next step is to create a decent JavaScript object that can bridge between methods in this BrowseController and a lazy-loadable JQuery JSTree. Maybe the next post can share some developments there.

No comments: