如何对调用 g.formatDate 的标签库进行单元测试?
How do I unit test a taglib that calls g.formatDate?
我有一个标签库调用 formatDate
:
out << g.formatDate(attrs)
在我的单元测试中,我有以下内容:
def output = applyTemplate('<tz:formatDate date="${date}"/>', [date: date])
当我 运行 测试时出现以下错误:
org.grails.taglib.GrailsTagException: [Byte array resource [test_1520620408798]:1] Error executing tag <tz:formatDate>: Cannot invoke method getTimeZone() on null object
at org.grails.gsp.GroovyPage.throwRootCause(GroovyPage.java:472)
at org.grails.gsp.GroovyPage.invokeTag(GroovyPage.java:415)
at test_1520620408798.run(test_1520620408798:15)
at org.grails.gsp.GroovyPageWritable.doWriteTo(GroovyPageWritable.java:162)
at org.grails.gsp.GroovyPageWritable.writeTo(GroovyPageWritable.java:82)
at grails.testing.web.GrailsWebUnitTest$Trait$Helper.renderTemplateToStringWriter(GrailsWebUnitTest.groovy:242)
at grails.testing.web.GrailsWebUnitTest$Trait$Helper.applyTemplate(GrailsWebUnitTest.groovy:227)
at grails.testing.web.taglib.TagLibUnitTest$Trait$Helper.applyTemplate(TagLibUnitTest.groovy:49)
at grails.testing.web.GrailsWebUnitTest$Trait$Helper.applyTemplate(GrailsWebUnitTest.groovy:212)
at grails.testing.web.taglib.TagLibUnitTest$Trait$Helper.applyTemplate(TagLibUnitTest.groovy:44)
at com.captivatelabs.grails.timezone.detection.FormatTagLibSpec.test offset client to server time - formatDate(FormatTagLibSpec.groovy:22)
Caused by: java.lang.NullPointerException: Cannot invoke method getTimeZone() on null object
at org.grails.plugins.web.taglib.FormatTagLib$_closure2.doCall(FormatTagLib.groovy:170)
at groovy.lang.Closure.call(Closure.java:414)
at org.grails.taglib.TagOutput.captureTagOutput(TagOutput.java:64)
at org.grails.taglib.TagLibraryMetaUtils.methodMissingForTagLib(TagLibraryMetaUtils.groovy:139)
at org.grails.taglib.NamespacedTagDispatcher.methodMissing(NamespacedTagDispatcher.groovy:59)
at com.captivatelabs.grails.timezone.detection.FormatTagLib$_closure1.doCall(FormatTagLib.groovy:14)
at groovy.lang.Closure.call(Closure.java:414)
at org.grails.gsp.GroovyPage.invokeTagLibClosure(GroovyPage.java:439)
at org.grails.gsp.GroovyPage.invokeTag(GroovyPage.java:364)
... 9 more
有人对此有什么想法吗?
您可以通过多种方式来协调这一点。一个在 https://github.com/jeffbrown/pietertest.
的项目中进行了演示
https://github.com/jeffbrown/pietertest/blob/master/grails-app/taglib/pieter/DemoTagLib.groovy
package pieter
class DemoTagLib {
static defaultEncodeAs = [taglib:'html']
static namespace = 'tz'
def formatDate = { attrs ->
out << g.formatDate(date: attrs.date, format: 'yyyy-MM-dd')
}
}
https://github.com/jeffbrown/pietertest/blob/master/src/test/groovy/pieter/DemoTagLibSpec.groovy
package pieter
import grails.testing.web.taglib.TagLibUnitTest
import org.grails.plugins.web.DefaultGrailsTagDateHelper
import spock.lang.Specification
class DemoTagLibSpec extends Specification implements TagLibUnitTest<DemoTagLib> {
Closure doWithSpring() {{ ->
grailsTagDateHelper DefaultGrailsTagDateHelper
}}
void "test date format"() {
given:
def date
Calendar cal = Calendar.instance
cal.with {
clear()
set MONTH, JULY
set YEAR, 1776
set DATE, 4
date = time
}
when:
def output = applyTemplate('<tz:formatDate date="${date}"/>', [date: date])
then:
output == '1776-07-04'
}
}
希望对您有所帮助。
我有一个标签库调用 formatDate
:
out << g.formatDate(attrs)
在我的单元测试中,我有以下内容:
def output = applyTemplate('<tz:formatDate date="${date}"/>', [date: date])
当我 运行 测试时出现以下错误:
org.grails.taglib.GrailsTagException: [Byte array resource [test_1520620408798]:1] Error executing tag <tz:formatDate>: Cannot invoke method getTimeZone() on null object
at org.grails.gsp.GroovyPage.throwRootCause(GroovyPage.java:472)
at org.grails.gsp.GroovyPage.invokeTag(GroovyPage.java:415)
at test_1520620408798.run(test_1520620408798:15)
at org.grails.gsp.GroovyPageWritable.doWriteTo(GroovyPageWritable.java:162)
at org.grails.gsp.GroovyPageWritable.writeTo(GroovyPageWritable.java:82)
at grails.testing.web.GrailsWebUnitTest$Trait$Helper.renderTemplateToStringWriter(GrailsWebUnitTest.groovy:242)
at grails.testing.web.GrailsWebUnitTest$Trait$Helper.applyTemplate(GrailsWebUnitTest.groovy:227)
at grails.testing.web.taglib.TagLibUnitTest$Trait$Helper.applyTemplate(TagLibUnitTest.groovy:49)
at grails.testing.web.GrailsWebUnitTest$Trait$Helper.applyTemplate(GrailsWebUnitTest.groovy:212)
at grails.testing.web.taglib.TagLibUnitTest$Trait$Helper.applyTemplate(TagLibUnitTest.groovy:44)
at com.captivatelabs.grails.timezone.detection.FormatTagLibSpec.test offset client to server time - formatDate(FormatTagLibSpec.groovy:22)
Caused by: java.lang.NullPointerException: Cannot invoke method getTimeZone() on null object
at org.grails.plugins.web.taglib.FormatTagLib$_closure2.doCall(FormatTagLib.groovy:170)
at groovy.lang.Closure.call(Closure.java:414)
at org.grails.taglib.TagOutput.captureTagOutput(TagOutput.java:64)
at org.grails.taglib.TagLibraryMetaUtils.methodMissingForTagLib(TagLibraryMetaUtils.groovy:139)
at org.grails.taglib.NamespacedTagDispatcher.methodMissing(NamespacedTagDispatcher.groovy:59)
at com.captivatelabs.grails.timezone.detection.FormatTagLib$_closure1.doCall(FormatTagLib.groovy:14)
at groovy.lang.Closure.call(Closure.java:414)
at org.grails.gsp.GroovyPage.invokeTagLibClosure(GroovyPage.java:439)
at org.grails.gsp.GroovyPage.invokeTag(GroovyPage.java:364)
... 9 more
有人对此有什么想法吗?
您可以通过多种方式来协调这一点。一个在 https://github.com/jeffbrown/pietertest.
的项目中进行了演示https://github.com/jeffbrown/pietertest/blob/master/grails-app/taglib/pieter/DemoTagLib.groovy
package pieter
class DemoTagLib {
static defaultEncodeAs = [taglib:'html']
static namespace = 'tz'
def formatDate = { attrs ->
out << g.formatDate(date: attrs.date, format: 'yyyy-MM-dd')
}
}
https://github.com/jeffbrown/pietertest/blob/master/src/test/groovy/pieter/DemoTagLibSpec.groovy
package pieter
import grails.testing.web.taglib.TagLibUnitTest
import org.grails.plugins.web.DefaultGrailsTagDateHelper
import spock.lang.Specification
class DemoTagLibSpec extends Specification implements TagLibUnitTest<DemoTagLib> {
Closure doWithSpring() {{ ->
grailsTagDateHelper DefaultGrailsTagDateHelper
}}
void "test date format"() {
given:
def date
Calendar cal = Calendar.instance
cal.with {
clear()
set MONTH, JULY
set YEAR, 1776
set DATE, 4
date = time
}
when:
def output = applyTemplate('<tz:formatDate date="${date}"/>', [date: date])
then:
output == '1776-07-04'
}
}
希望对您有所帮助。