假设我有一个使用FormatTagLib
的TagLib:
class MyTagLib {
def something = {attrs, body ->
def format = new FormatTagLib()
out << format.formatDate(attrs.date, format: 'HH:mm')
}
}
我为这个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...
}
}
为什么此代码会引发formatDate
的异常?
org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Tag [formatDate] does not exist. No corresponding tag library found.
答案 0 :(得分:1)
有几件事:
我认为一个有效的例子是:
class MyTagLib {
static namespace = "myTags"
def something = { attrs, body ->
out << g.formatDate(date: attrs.date, format: 'HH:mm')
}
}
测试:
@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)
}
}