1

カスタム アーティファクト インスタンスを使用するカスタム TagLib を含むプラグインを作成しました。プラグインがアプリケーションに含まれている場合、taglib は期待どおりに機能します。ただし、統合テストを書くことができません。

カスタム アーティファクト タイプが「Foo」で、アーティファクト ハンドラ クラスがFooArtefactHandler

(簡略化された)FooTagLibクラスは次のようになります。

class FooTagLib {

    static namespace = "bar"

    def eachFoo = { attrs, body ->
        grailsApplication.fooClasses.each { foo ->
            out << body()
        }
    }
}

関連するFooTagLibTestsクラスは次のようになります。

import grails.test.mixin.*

@TestFor(FooTagLib)
class FooTagLibTests {

    void testEachFoo() {
        grailsApplication.registerArtefactHandler(new FooArtefactHandler())
        // Classes AFoo and BFoo are in the test/integration folder
        grailsApplication.addArtefact(FooArtefactHandler.TYPE, AFoo)
        grailsApplication.addArtefact(FooArtefactHandler.TYPE, BFoo)
        // just to check if artefacts are correctly loaded
        assert grailsApplication.fooClasses.length == 2

        assert applyTemplate("<bar:eachFoo>baz</bar:eachFoo>") == "bazbaz"
    }
}

このテストを実行すると、結果は次のようになります。

| Failure:  testeachFoo(com.my.package.FooTagLibTests)
|  org.codehaus.groovy.grails.web.taglib.exceptions.GrailsTagException: Error executing tag <bar:eachFoo>: No such property: fooClasses for class: org.codehaus.groovy.grails.commons.DefaultGrailsApplication

taglib 内のgrailsApplicationは、テスト内のものと同じインスタンスではないようです。誰かが私にこれを説明できますか?私はここで何か悪いことをしていますか?

4

1 に答える 1

1

これが統合テストである場合は@TestFor、代わりに、GroovyPagesTestCasegrailsApplicationを拡張して宣言する必要はありません。

class FooTagLibTests extends GroovyPagesTestCase {

    def grailsApplication

    void testEachFoo() {
        grailsApplication.registerArtefactHandler(new FooArtefactHandler())
        // Classes AFoo and BFoo are in the test/integration folder
        grailsApplication.addArtefact(FooArtefactHandler.TYPE, AFoo)
        grailsApplication.addArtefact(FooArtefactHandler.TYPE, BFoo)
        // just to check if artefacts are correctly loaded
        assert grailsApplication.fooClasses.length == 2

        assert applyTemplate("<bar:eachFoo>baz</bar:eachFoo>") == "bazbaz"
    }
}

これは、TestForアノテーションがgrailsApplicationのインスタンス(単体テストで使用される)をモックするためです。

于 2012-11-29T17:49:08.947 に答える