Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/unit-testing/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Unit testing grails—如何使用applyTemplate测试将非字符串变量传递给taglib属性映射?_Unit Testing_Grails_Taglib - Fatal编程技术网

Unit testing grails—如何使用applyTemplate测试将非字符串变量传递给taglib属性映射?

Unit testing grails—如何使用applyTemplate测试将非字符串变量传递给taglib属性映射?,unit-testing,grails,taglib,Unit Testing,Grails,Taglib,grails 3.3.9,+标记库 我创建了一个新的标记库,如下所示 import java.time.LocalDateTime import java.time.format.DateTimeFormatter class JavaDateTimeTagLib { static defaultEncodeAs = [taglib:'html'] //static encodeAsForTags = [tagName: [taglib:'html'], otherTagNam

grails 3.3.9,+标记库

我创建了一个新的标记库,如下所示

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

class JavaDateTimeTagLib {
    static defaultEncodeAs = [taglib:'html']
    //static encodeAsForTags = [tagName: [taglib:'html'], otherTagName: [taglib:'none']]
    static encodeAsForTags = [testCall: [taglib:'none']]

    static namespace = "jdt"        //java8 date time name space for tags

    def testCall = { attrs ->

        def p1 = attrs.p1
        def p2 = attrs.p2
        out << "p1:'$p1' with class ${p1.getClass()}"
        out << "p2:'$p2' with class ${p2.getClass()}"
    }
}
class JavaDateTimeTagLibSpec extends Specification implements TagLibUnitTest<JavaDateTimeTagLib> {

    def setup() {
    }

    def cleanup() {
    }

    /**
     * restriction params must be quoted values - esentially strings
     * taglib has to take that and do any conversions in the taglib
     * output by defaults is encoded html using std codec
     */
    void "call displayDateTime tag "() {
        given:

        String result = applyTemplate('<jdt:testCall p1="p1-string" p2="$now"  />' , [now:LocalDateTime.now()])

        when :
        println "$result "

        then:
        result
    }
}
如果使用p2=“$ldt”引用p2变量,并将sname映射为[ldt:LocalDateTime.now()],那么测试将正常工作-但是传递给ATTR映射的类型是GStringImpl

然而,阅读OCI指南

它在第4页暗示您可以将attrs.employees作为域对象列表传递,并在标记中使用它

但是使用测试无法调用它,因为所有内容都必须被字符串引用——这使得它成为GStringImpl


如何将非字符串变量从appyTemplate传递到taglibs属性映射(…我假设相同的restiction适用于live gsp,而不仅仅是测试框架)

hah!这真是一件小事

如果您更改测试并编写此

    String result = applyTemplate('<jdt:testCall p1="p1-string" p2="${now}"  />' , [now:LocalDateTime.now()])
String result=applyTemplate(“”,[now:LocalDateTime.now()]))
在映射中的变量键名周围加上大括号-然后将类型化变量的实际值传递到taglib中。因此,如果查看attrs.p2值,它将具有实际的LocalDateTime实例

现在非常确定applytemplate是如何工作的还不确定,但希望它尝试像groovy shell那样对字符串进行求值-因此需要使用单个“like” 希望这是有道理的

天哪,有时候会让你的头爆炸

    String result = applyTemplate('<jdt:testCall p1="p1-string" p2="${now}"  />' , [now:LocalDateTime.now()])