如何对调用g.formatDate的taglib进行单元测试?

时间:2018-03-09 23:46:10

标签: unit-testing grails taglib

我有一个标记库,可以调用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

有没有人对此有任何想法?

1 个答案:

答案 0 :(得分:1)

有很多方法可以协调这个。其中一个在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'
    }
}

我希望有所帮助。