user800014
user800014

Reputation:

Why tag lib test throws GrailsTagException?

Let's say that I have one TagLib that use FormatTagLib:

class MyTagLib {

  def something = {attrs, body ->
    def format = new FormatTagLib()
    out << format.formatDate(attrs.date, format: 'HH:mm')
  }

}

and I wrote a unit test for this taglib:

class MyTagLibTests extends TagLibUnitTestCase {

  //setUp() and tearDown() ommited

  void testMyTagLib() {
    tagLib = new MyTagLib()
    tagLib.something(date: Date.parse('20/04/2012 08:00','dd/MM/yyyy HH:mm'))
    assertEquals('08:00', out.toString()) //out is mocked...
  }

}

Why this code throws exception for formatDate?

org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Tag [formatDate] does not exist. No corresponding tag library found.

Upvotes: 0

Views: 1131

Answers (1)

Jon Palmer
Jon Palmer

Reputation: 171

A couple of things:

  1. You don't need to instantiate the FormatTagLib in your new taglib
  2. There is a bug in your tag lib, FormatDate takes a map not a date and a map
  3. Grails makes testing taglibs a lot simpler if you use the built in features.

I think a working example is this:

class MyTagLib {
    static namespace = "myTags"

    def something = { attrs, body ->
        out << g.formatDate(date: attrs.date, format: 'HH:mm')
    }
}

with the test:

@TestFor(MyTagLib)
class MyTagLibTests  {
    void testMyTagLib() {
        def templateOut = applyTemplate('<myTags:something date="${date}"/>', [date: new Date(12, 3, 20, 8, 0)])
        assertEquals('08:00', templateOut)
    }
}

Upvotes: 1

Related Questions