0

好的,我在自定义 taglib 中有一个自定义 taglib,如下所示:

def preference = { attrs, body ->

        def sliderTaglib = grailsApplication.mainContext.getBean('com.myCustom.sliderTagLib')
        sliderTaglib.slider.call(attrs, body)

}

由于我正在使用grailsApplication.mainContext.getBean(),我如何在单元测试中模拟它?我的测试抱怨:

grails Error executing tag No bean named is defined...

我尝试了各种方法来模拟它,但无济于事。但是,当我运行应用程序时,它可以正常运行,只是测试失败了。我正在使用 grails 2.3.9 和 spock。:(

我的测试如下所示:

void "taglib should output a slider"() {
    when:
    def result = applyTemplate("""
        <a:preference class='slider'/>
    """)
    then:
    result == "<div class='slider'></div>"
}
4

1 回答 1

0

您可以使用模拟标签库

@TestMixin(GroovyPageUnitTestMixin)
class MultipleTagLibSpec extends Specification {

    void "test multiple tags"() {
        given:
        mockTagLib(sliderTagLib)

        expect:
        // …
    }
}

您不应该sliderTagLib在应用程序上下文中查找 bean。您只需调用 taglib 即可taglib-namespacetaglib-method.

例如,Grails 标签库位于g命名空间中。因此像这样使用它。

def preference = { attrs, body ->
    out << g.link(...)
    // in your case
    yourNamespace.slider(....)
}
于 2014-06-16T12:14:24.210 回答