Testing Tag Libraries

Unit Testing

TODO

Integration Testing

Testing tag libraries is actually pretty trivial because when a tag is invoked as a method it returns its result as a string. So for example if you have a tag library like this:

class FooTagLib {
   def bar =  { attrs, body ->
   	   out << "<p>Hello World!</p>"
   }

def bodyTag = { attrs, body -> out << "<${attrs.name}>" out << body() out << "</${attrs.name}>" } }

The tests would look like:

class FooTagLibTests extends GroovyTestCase {

void testBarTag() { assertEquals "<p>Hello World!</p>", new FooTagLib().bar(null,null) }

void testBodyTag() { assertEquals "<p>Hello World!</p>", new FooTagLib().bodyTag(name:"p") { "Hello World!" } } }

Notice that for the second example, {{testBodyTag}}, we pass a closure that returns the body of the tag. This is handy for representing the body as a String.

1 Comment

  • Gravatar
    Doesn't work: Returns FAILED
    Jun 07, 2009 22:06 PM loxal

Post a Comment