align-toparrow-leftarrow-rightbackbellblockcalendarcamerachatcheckchevron-downchevron-leftchevron-rightchevron-small-downchevron-small-leftchevron-small-rightchevron-small-upchevron-upcircle-with-crosscrosseditfacebookglobegoogleimagesinstagramlocation-pinmagnifying-glassmailmoremuplabelShape 3 + Rectangle 1outlookpersonplusImported LayersImported LayersImported Layersshieldstartwitteryahoo

taglib testing

From: user 1.
Sent on: Tuesday, February 26, 2008 3:35 PM
I was talking with Brad about how to test grail taglibs, and we didn't
come up with a solution. I figured it out now, and wanted to send it out
to you guys because it is so 'groovy'.

Consider that you've got a simple taglib:

class SomeTagLib {

   def doit = { attrs ->
     out << "foobar!"
   }
}

the question was: how to do you test this? It seems difficult, because
you've got to somehow stick a StringWriter or something in the place of
'out', but where is 'out' defined? (I originally thought it was
'System.out', but it isn't)

Here is the solution I came up with:

class SomeTagLibTest {

   def testDoIt {
     def sw = new StringWriter();
     def pw = new PrintWriter(sw);

     SomeTagLib.metaClass­.getOut << { -> out }

     def tag = new SomeTagLib();
     taglib.doit([:])

     assertEquals("foobar­!", sw.toString())
   }

}

Understanding this has totally opened my eyes to how grails is doing a
lot of things behind the scenes.

Eric...

Sign up

Meetup members, Log in

By clicking "Sign up" or "Sign up using Facebook", you confirm that you accept our Terms of Service & Privacy Policy