gpt4 book ai didi

unit-testing - 如何对调用 g.formatDate 的标签库进行单元测试?

转载 作者:行者123 更新时间:2023-12-02 13:50:24 24 4
gpt4 key购买 nike

我有一个调用 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'
}
}

希望对您有所帮助。

关于unit-testing - 如何对调用 g.formatDate 的标签库进行单元测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49203808/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com