Grails and tests - Part II: Testing dynamic taglibs using XmlSlurper
Published by peter October 2nd, 2007 in grails, groovy, testing.A couple of days ago I wrote my first post on TDD using Grails. Now let's move on to something more interesting: testing dynamic taglibs.
In another post I showed how to create a simple tag. Which looked like this:
When generating this test using the "grails create-tag-lib" command Grails also created a skeleton for the corresponding test:
Testing calling the taglib is simple, as opposed to JspTags they don't rely on a container. You can just call the closure with the necessary attributes:
Since the textile taglib converts the given textile markup to HTML we'll need something to parse the resulting output. Enter XmlSlurper. XmlSluper is a powerful XML parser with lower overheads than XmlParser due to lazy evaluation. XmlSlurper only supports read operations. Using XmlSlurper we can actually test the output of the tag.
We'll enter something like this:
-
# ab
-
# cd
-
# ef
Which should result in something like this:
And here the actual implementation of the test:
-
-
-
-
assertEqualsTr("ab", ol.li[0].text())
-
assertEqualsTr("cd", ol.li[1].text())
-
assertEqualsTr("ef", ol.li[2].text())
-
}
-
-
assertEquals(expected, actual.trim())
-
}
-
}
Surely I should have started out with the test in first place, but hey... I first had to see how Grails taglibs work in this case! Since testing is this simple I'll actually consider doing so next time.




















0 Responses to “Grails and tests - Part II: Testing dynamic taglibs using XmlSlurper”
Please Wait
Leave a Reply